Files
OldPeopleHome/web/server.js
vickihe 8a077ad253 update
2019-09-06 21:29:12 +08:00

34 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
var http = require('http');
var fs = require('fs');
var url = require('url');
// 创建服务器
http.createServer(function (request, response) {
// 解析请求,包括文件名
var pathname = url.parse(request.url).pathname;
// 输出请求的文件名
console.log("Request for" + pathname + "received.");
// 从文件系统中读取请求的文件内容
fs.readFile(pathname.substr(1), function (error, data) {
if(error){
console.log(error);
// HTTP状态码404NOT FOUND
// Content Typetext/html
response.writeHead(404,{'Content-Type': 'text/html'});
}else{
// HTTP 状态码200OK
// Content Type: text/html
response.writeHead(200,{'Content-Type': 'text/html'});
console.log("11111");
// 响应文件内容
response.write(data.toString());
}
// 发送响应数据
response.end();
});
}).listen(8080);
console.log('server running at http://39.105.65.209:8080/');