Created
February 2, 2020 19:47
-
-
Save carzacc/22cfe51227660d02aa6cf3e44d4e3555 to your computer and use it in GitHub Desktop.
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
var express = require('express') | |
var multer = require('multer') | |
var fs = require('fs'); | |
var upload = multer({ dest: 'uploads/' }) | |
var app = express() | |
app.post('/upload', upload.single("picture"), function (req,res) { | |
console.log("Received file" + req.file.originalname); | |
var src = fs.createReadStream(req.file.path); | |
var dest = fs.createWriteStream('uploads/' + req.file.originalname); | |
src.pipe(dest); | |
src.on('end', function() { | |
fs.unlinkSync(req.file.path); | |
res.json('OK: received ' + req.file.originalname); | |
}); | |
src.on('error', function(err) { res.json('Something went wrong!'); }); | |
}) | |
let port = process.env.PORT || 3000; | |
app.listen(port, function () { | |
return console.log("Started file upload server on port " + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment