-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeserver.js
46 lines (38 loc) · 1.2 KB
/
nodeserver.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
const express = require('express');
const multer = require('multer');
const path = require('path');
const server = express();
const uploadFolder = "/uploaded_files/";
// View Engine Setup
server.set("views", path.join(__dirname, "views"));
server.set("view engine", "ejs");
server.get("/", function(req, res){
res.render("home");
});
// uploaded files are saved in the folder specified by uploadFolder
const upload = multer({
dest: __dirname + uploadFolder
});
server.use(express.static('public'));
// "dbfiles" is the key of the http payload
server.post('/uploadFiles', upload.array('dbfiles', 5), function(request, respond) {
if(request.files) {
request.files.forEach(file => {
time = new Date(new Date().toUTCString());
console.log(time);
console.log(file);
// save the file
var fs = require('fs');
fs.rename(__dirname + uploadFolder + file.filename, __dirname + uploadFolder + file.originalname, function(err) {
if (err) console.log('ERROR: ' + err);
});
});
}
respond.end('Files uploaded!');
});
const PORT = 8027;
const hostname = '0.0.0.0';
server.listen(PORT, hostname, function(error) {
if (error) throw error
console.log(`Server running at http://${hostname}:${PORT}/`);
});