-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.js
53 lines (42 loc) · 1.75 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const express = require('express'); // Express Web Server
const busboy = require('connect-busboy'); // Middleware to handle the file upload https://github.com/mscdex/connect-busboy
const path = require('path'); // Used for manipulation with path
const fs = require('fs-extra'); // Classic fs
const app = express(); // Initialize the express web server
app.use(busboy({
highWaterMark: 2 * 1024 * 1024, // Set 2MiB buffer
})); // Insert the busboy middle-ware
const uploadPath = path.join(__dirname, 'fu/'); // Register the upload path
fs.ensureDir(uploadPath); // Make sure that he upload path exits
/**
* Create route /upload which handles the post request
*/
app.route('/upload').post((req, res, next) => {
req.pipe(req.busboy); // Pipe it trough busboy
req.busboy.on('file', (fieldname, file, filename) => {
console.log(`Upload of '${filename}' started`);
// Create a write stream of the new file
const fstream = fs.createWriteStream(path.join(uploadPath, filename));
// Pipe it trough
file.pipe(fstream);
// On finish of the upload
fstream.on('close', () => {
console.log(`Upload of '${filename}' finished`);
res.redirect('back');
});
});
});
/**
* Serve the basic index.html with upload form
*/
app.route('/').get((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="upload" 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();
});
const server = app.listen(3200, function () {
console.log(`Listening on port ${server.address().port}`);
});