-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·58 lines (52 loc) · 1.43 KB
/
Copy pathapp.js
File metadata and controls
executable file
·58 lines (52 loc) · 1.43 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
56
57
58
const http = require('http');
const fs = require('fs');
const URL = require('url');
const origin = {
'Access-Control-Allow-Origin': 'http://localhost:8000',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'X-Response1'
}
function createServer (app, port) {
let server = http.createServer(app);
server.listen(port, function () {
console.log('server started port ' + port)
});
}
createServer((req, res) => {
const {headers, method} = req;
const url = URL.parse(req.url);
const paths = ['/build','/public','/dist','/lib'];
if (method == 'POST') {
var jsonString=[];
req.on('data', (chunk) => {
jsonString.push(chunk);
}).on('end', function () {
console.log(Buffer.concat(jsonString).toString());
});
}
if (url.pathname === '/') {
fs.readFile(__dirname + '/index.html', (err, data) => {
if (err) {
res.writeHead(404, {'Content-Type':'text/html'});
res.write('<h1>' + err + '</h1>');
res.end();
return;
}
res.writeHead(200, Object.assign({'Content-Type': 'text/html; charset=utf-8'}, origin));
res.write(data);
res.end()
});
} else {
fs.readFile(__dirname + url.pathname, (err, data) => {
if (err) {
res.writeHead(404, {'Content-Type':'text/html'});
res.write('<h1>' + err + '</h1>');
res.end();
return;
}
res.writeHead(200, Object.assign({'Content-Type': 'text/html; charset=utf-8'}, origin));
res.write(data);
res.end()
});
}
}, 9000);