-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
31 lines (25 loc) · 847 Bytes
/
server.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
var http = require('http'),
fs = require('fs');
http.createServer(function (req, res){
// 主页
if (req.url == "/") {
fs.createReadStream(`${__dirname}/index.html`).pipe(res);
}
else if (req.url.match(/.html|.js|.jpg|.css/)) {
fs.createReadStream(__dirname + req.url).pipe(res);
}
else if (req.url == "/getJson") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("{ a: 'a', b: 'b' }");
}
else if (req.url == "/getJson1") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("{ name: 'Json1', url: 'getJson1' }");
}
// 404错误
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 error! File not found.");
}
}).listen(80, '127.0.0.1');
console.log('Server running on port 80');