第一个node.js服务器应用

首先我们编写js文件如下
var http=require('http');

http.createServer(function(request,response){
	//发送HTTP头部
	//HTTP状态值:300:OK
	//内容类型: text/plain
	response.writeHead(200,{'Content-Type':'text/plain'});
	//发送响应数据“Hello World”
	response.end("Hello world\n");

}).listen(8888);
//终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

然后使用node命令执行以上代码,就可以创建一个可以工作的HTTP服务器了


控制台打印成功说明js文件执行成功

之后我们在浏览器中访问 http://127.0.0.1:8888


成功接收服务器发送的数据

猜你喜欢

转载自blog.csdn.net/djz917/article/details/80701404