-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
55 lines (49 loc) · 1.98 KB
/
backend.js
File metadata and controls
55 lines (49 loc) · 1.98 KB
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
54
55
var formidable = require('formidable'),
http = require('http'),
util = require('util');
const host = 'localhost';
const port = 8001;
const requestListener = function (req, res) {
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if ( req.method === 'OPTIONS' ) {
res.writeHead(200);
res.end();
return;
}
// This if statement is here to catch form submissions, and initiate multipart form data parsing.
if (req.method.toLowerCase() == 'post') {
// Instantiate a new formidable form for processing.
var form = new formidable.IncomingForm();
// form.parse analyzes the incoming stream data, picking apart the different fields and files for you.
form.parse(req, function(err, fields, files) {
if (err) {
// Check for and handle any errors here.
console.error(err.message);
return;
}
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
//check for files
if (!files | (Object.keys(files).length == 0)) {
console.log('empty');
return;
} else {
console.log('file yay');
}
//log received data
console.log("new request received:")
console.log(util.inspect({fields: fields, files: files}))
// This last line responds to the form submission with a list of the parsed data and files.
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`dreckweg.dresden.de-mock is running on http://${host}:${port}`);
});