-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver2.js
More file actions
40 lines (32 loc) · 1.02 KB
/
server2.js
File metadata and controls
40 lines (32 loc) · 1.02 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
const path = require("path");
const fs = require("fs");
const http = require("http");
const url = require("url");
const mime = require("mime");
function readStaticFile(res, pathName) {
const mimeType = mime.getType(pathName);
if (!mimeType) return;
fs.readFile(path.resolve(__dirname, `./${pathName}`), (err, data) => {
if (err) {
res.writeHead(404, { "Content-Type": "text/plain" });
res.write("404 NOT FOUND");
res.end();
} else {
res.writeHead(200, { "Content-Type": mimeType });
res.write(data);
res.end();
}
});
}
const server = http.createServer(function (req, res) {
// http://nodejs.cn/api/url.html
let pathName = url.parse(req.url).pathname;
if (pathName === "/") pathName = "index.html";
res.setHeader("Access-Control-Allow-Origin", "*");
// res.writeHead(200, { "Content-Type": mime.getType(pathName) });
// 读取静态文件
readStaticFile(res, pathName);
});
server.listen(5000, () => {
console.log("Server running at http://127.0.0.1:5000/");
});