Skip to content

Instantly share code, notes, and snippets.

@coloredlambda
Created April 3, 2018 08:21
Show Gist options
  • Save coloredlambda/752019ca7de24184efdceff20d9384ad to your computer and use it in GitHub Desktop.
Save coloredlambda/752019ca7de24184efdceff20d9384ad to your computer and use it in GitHub Desktop.
Multer
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const multer = require('multer');
const storageConfiguration = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, `${__dirname}/upload`);
},
filename: (req, file, callback) => {
let name = `ourFile${path.extname(file.originalname)}`;
callback(null, name);
}
});
const upload = multer({ storage: storageConfiguration });
const app = express();
app.get('/api', (req, res) => {
res.send('Welcome to our upload server. Have a nice ride')
});
app.post('/api/upload', upload.single('file'), (req, res) => {
res.send('File received')
});
app.get('*', (req, res) => {
res.send('Mmmm, you must be lost. We have no such route')
});
app.listen(8080, () => {
console.log('Server started on http://localhost:8080')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment