我想模拟客户端 - 服务器节点。
这是我的 server.js
var http = require("http");
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data){
/* if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404 1");
}
*/
response.writeHead(200, {'Content-Type': 'text/html'});
// response.write("data terbaca 2");
response.write(data, "utf8");
});
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world2 + ' + __dirname + ' ' + path);
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404 3");
break;
}
response.end();
});
server.listen(8000);
这是我的客户 html
<html>
<head></head>
<body>Ini Socket html lohhh</body>
</html>
我的目录结构在 D:/cobaa 1. server.js 2. socket.html
我节点服务器,并访问 localhost:8000/socket.html 并得到
你好 world2 + D:\cobaa /socket.html
任何人都可以给我解决方案吗?非常感谢 :)