Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonahgeek/b9589446d761dbe52539b5fd2081a881 to your computer and use it in GitHub Desktop.
Save jonahgeek/b9589446d761dbe52539b5fd2081a881 to your computer and use it in GitHub Desktop.
Uploading files and serve Directory Listing Using NodeJS

On your base app directory, create a folder as such:

mkdir -p public/uploads

Install multer in order to handle multipart/form-data

npm i multer

Import multer before you can use it

const multer = require('multer');

Initiate disk storage

var storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, './public/uploads')
    },
    filename: (req, file, cb) => {
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
    }
});

//will be using this for uplading
const upload = multer({ storage: storage });

Add this to serve static assets app.use('/ftp', express.static('public'), serveIndex('public', {'icons': true}));

Put this below your other middleware functions. app.use(express.static('public'));

Great Job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment