Skip to content

Instantly share code, notes, and snippets.

@Belrestro
Last active February 24, 2019 20:55
Show Gist options
  • Save Belrestro/322283e38b816b4348c20aa0699e9ad6 to your computer and use it in GitHub Desktop.
Save Belrestro/322283e38b816b4348c20aa0699e9ad6 to your computer and use it in GitHub Desktop.
const http = require("http");
const path = require("path");
const fs = require("fs");
const busboy = require('connect-busboy');
const express = require("express");
const STORE_FOLDER = 'public';
const app = express();
app.use(busboy({
highWaterMark : 2 * 1024 * 1024 // alot
}))
const httpServer = http.createServer(app);
const PORT = process.env.PORT || 3000;
httpServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
app.use(express.static(STORE_FOLDER));
app.post('/upload', function (req, res) {
req.pipe(req.busboy);
req.busboy.on('file', (_, file, name) => {
const fstream = fs.createWriteStream(`./${STORE_FOLDER}/${name}`);
file.pipe(fstream);
fstream.on('close', () => {
res.send('Download complete')
});
fstream.on('error', () => {
res.send('Download failed')
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment