Created
December 16, 2019 13:11
-
-
Save frankhn/3098e31bf189fb38f53778993cde577c 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 http = require('http'); | |
const express = require('express') | |
const path = require('path') | |
const fs = require('fs') | |
const bodyParser = require('body-parser') | |
const Busboy = require('busboy') | |
let app = express(); | |
app.use(bodyParser.json()) | |
app.get('/', (req, res) => { | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">'); | |
res.write('<input type="file" name="filetoupload"><br>'); | |
res.write('<input type="submit">'); | |
res.write('</form>'); | |
return res.end(); | |
}) | |
app.post('/fileupload', (req, res) => { | |
var busboy = new Busboy({ headers: req.headers }); | |
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => { | |
var saveTo = path.join(__dirname, '/images/' + filename); | |
file.pipe(fs.createWriteStream(saveTo)); | |
}); | |
busboy.on('finish', () => { | |
res.writeHead(200, { 'Connection': 'close' }); | |
res.end("That's all folks!"); | |
}); | |
return req.pipe(busboy); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment