Skip to content

Instantly share code, notes, and snippets.

@frankhn
Created December 16, 2019 13:11
Show Gist options
  • Save frankhn/3098e31bf189fb38f53778993cde577c to your computer and use it in GitHub Desktop.
Save frankhn/3098e31bf189fb38f53778993cde577c to your computer and use it in GitHub Desktop.
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