Created
March 23, 2020 08:59
-
-
Save pprathameshmore/981d4ebf1e1abf229eea7a08f93da513 to your computer and use it in GitHub Desktop.
This file contains 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 app = express(); | |
const path = require('path'); | |
const multer = require('multer'); | |
const port = 3000; | |
app.use('/uploads', express.static(path.join(__dirname, '/uploads'))); | |
const storage = multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, 'uploads'); | |
}, | |
filename: (req, file, cb) => { | |
console.log(file); | |
cb(null, Date.now() + path.extname(file.originalname)); | |
} | |
}); | |
const fileFilter = (req, file, cb) => { | |
if (file.mimetype == 'image/jpeg' || file.mimetype == 'image/png') { | |
cb(null, true); | |
} else { | |
cb(null, false); | |
} | |
} | |
const upload = multer({ storage: storage, fileFilter: fileFilter }); | |
//Upload route | |
app.post('/upload', upload.single('image'), (req, res, next) => { | |
try { | |
return res.status(201).json({ | |
message: 'File uploded successfully' | |
}); | |
} catch (error) { | |
console.error(error); | |
} | |
}); | |
app.listen(port, () => console.log(`Hello world app listening on port ${port}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment