var http = require('http');
function start() {
http.createServer(function(request, response) {
console.log('request received');
response.writeHead(200, {'Content-Type' : 'text/plain'});
response.write('hello node');
response.end();
}).listen(8888);
}
console.log('server has started');
以上是我模拟的一个http服务器的程序
index.js
var server = require("./server");
server.start();
以上是模拟的客户端程序
接着运行该程序:node index.js
解决办法:
在server.js中加入exports.start=start;
var http = require('http');
function start() {
http.createServer(function(request, response) {
console.log('request received');
response.writeHead(200, {'Content-Type' : 'text/plain'});
response.write('hello node');
response.end();
}).listen(8888);
}
exports.start = start;
console.log('server has started');
这时再运行node index.js
在浏览器中输入http://localhost:8888/
本文介绍了一个简单的Node.js HTTP服务器实现过程。通过创建并导出一个名为`start`的函数来启动服务器,监听8888端口,并响应客户端请求。此外,还介绍了如何在客户端文件中调用此服务器模块。
2920

被折叠的 条评论
为什么被折叠?



