const express = require("express")
      const path = require("path")
      const multer = require("multer")
      const app = express()
      // View Engine Setup
      app.set("view engine","ejs")
      // var upload = multer({ dest: "Upload_folder_name" })
      // If you do not want to use diskStorage then uncomment it
      var storage = multer.diskStorage({
        destination: function (req, file, cb) {
        // Uploads is the Upload_folder_name
        cb(null, "uploads")
        filename: function (req, file, cb) {
        cb(null, file.fieldname + "-" +".jpg")
      // Define the maximum size for uploading
      // picture i.e. 1 MB. it is optional
      const maxSize = 1 * 1000 * 1000;
      var upload = multer({
        storage: storage,
        limits: { fileSize: maxSize },
        fileFilter: function (req, file, cb){
        // Set the filetypes, it is optional
        var filetypes = /jpeg|jpg|png/;
        var mimetype = filetypes.test(file.mimetype);
        var extname = filetypes.test(path.extname(
        if (mimetype && extname) {
          return cb(null, true);
        cb("Error: File upload only supports the "
          + "following filetypes - " + filetypes);
      // mypic is the name of file attribute
     "/uploadProfilePicture",function (req, res, next) {
        // Error MiddleWare for multer file upload, so if any
        // error occurs, the image would not be uploaded!
        upload(req,res,function(err) {
        if(err) {
          // ERROR occured (here it can be occured due
          // to uploading image of size greater than
          // 1MB or uploading different file type)
        else {
          // SUCCESS, image successfully uploaded
          res.send("Success, Image uploaded!")
      // Take any port number of your choice which
      // is not taken by any other process
      app.listen(8080,function(error) {
        if(error) throw error
        console.log("Server created Successfully on PORT 8080")

      Javascript language logo
      your recipe card header background
      File Upload

      React Project

      The following code sets up an Express application with two views: one for signup and one for uploading a profile picture.

      First, the views are defined. ejs is chosen as the view engine.

      Next, the uploader is created. storage is set to use the diskStorage module, and limits are set to limit the size of the uploaded file to 1MB.

      fileFilter is a function that checks the file type and determines whether or not to send the file. If the file is a JPEG, JPEG2000, or PNG, then it is sent. If the file is not one of these types, then an error is returned.

      The upload() method is called to upload the file. The first parameter is the request object, and the second parameter is the response object. The first parameter is an object containing the details of the file being uploaded, and the second parameter is the response object. The function called in the second parameter is called whenever an error occurs. If an error occurs, then the error is sent as the first parameter and the response object is not modified. Otherwise, the upload() method returns a success response and sends "Success, Image uploaded!" to the response object.


        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.