Created
April 3, 2018 08:21
-
-
Save coloredlambda/752019ca7de24184efdceff20d9384ad to your computer and use it in GitHub Desktop.
Multer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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