Table of contents
ExpressWebJs is a powerful and flexible Node FrameWork for building efficient and scalable backend services for modern applications. It is designed to be easy to use and flexible, allowing you to build a wide range of backend services.
ExpressWebJs has a built-in module for handling file uploads called FileProcessor
. With this module, you can easily accept file submissions from your users and store them locally or stream them directly to cloud storage services like Amazon S3, Cloudinary, or Google Cloud Storage
To use file uploads in ExpressWebJs, you will need to first set up a route in your application using the post
method of ExpressWebJs router. Here is an example of how you can do this:
Route.post("/upload","FileUploadController@upload");
In the above example, the route is linked to FileUploadController
at the upload
method. Inside this method, you can use the FileProcessor
module from ExpressWebJs to process the uploaded files. Here is an example of how you might use FileProcessor
to handle file uploads:
import {FileProcessor} from "Elucidate/FileProcessor";
import {Request, Response} from "Config/http";
import {BaseController} from "./BaseController";
export class FileUploadController extends BaseController{
public async upload(req:Request, res:Response){
const fileProcessor = new FileProcessor(req.files);
const files = fileProcessor.fieldName("file");
if(files && !Array.isArray(files)){
await files.mv(`App/Storage/${files.name}`).catch((error) => {
throw new Error(error);
});
return this.response.OK(res, "file successfully uploaded");
}
return this.response
.EXPECTATION_FAILED(res,"Invalid file provided");
}
}
Multiple file uploads:
From the above example, you will notice that you can also accept multiple files. All you have to do is to loop through files and save.
import {FileProcessor} from "Elucidate/FileProcessor";
import {Request, Response} from "Config/http";
import {BaseController} from "./BaseController";
export class FileUploadController extends BaseController{
public async upload(req:Request, res:Response){
const fileProcessor = new FileProcessor(req.files);
const files = fileProcessor.fieldName("file");
if(files && Array.isArray(files)){
for await (let file of files) {
await file.mv(`App/Storage/${file.name}`).catch((error) =>{
throw new Error(error);
});
}
return this.response.OK(res, "files successfully uploaded");
}
return this.response
.EXPECTATION_FAILED(res,"Invalid file provided");
}
}
File Validation:
Files can be validated by specifying the rules for the file mime type and the file size, while ExpressWebJs performs the validations implicitly.
import {FileProcessor} from "Elucidate/FileProcessor";
import {Request, Response} from "Config/http";
import {BaseController} from "./BaseController";
export class FileUploadController extends BaseController{
public async upload(req:Request, res:Response){
const fileProcessor = new FileProcessor(req.files);
const files = fileProcessor.fieldName("file");
if(files && !Array.isArray(files)){
const validation = await req.validate(files, {
size: "required|min:2|max:626140",
mimetype: ["required", { in: ["image/png", "image/jpg"] }],
});
if (!validation.success)
return this.response.EXPECTATION_FAILED(
res, {data: validation}
);
await files.mv(`App/Storage/${files.name}`).catch((error) => {
throw new Error(error);
});
return this.response.OK(res, "file successfully uploaded");
}
return this.response
.EXPECTATION_FAILED(res,"Invalid file provided");
}
}
Once you have set up your file upload route and controller in ExpressWebJs, you can test it out by sending a POST
request to the route with some files attached. You can use a tool like Postman to do this, or you can build a simple form in your application to allow users to submit files. With the file processor module in ExpressWebJs, it’s easy to accept and process file submissions from your users. Whether you want to store files locally or stream them to the cloud, the module provides everything you need to get started.
To learn more about ExpressWebJs, check out the official documentation. Kindly Join the ExpressWebJs community on Discord.
You can follow ExpressWebJs on Twitter @expresswebjs and don’t forget to star ExpressWebJs Project on GitHub