//导入一个http对象
var http = require('http');
//创建一个http服务器,参数request表示客户端向服务端发送的请求,response表示服务端向客户端的回应
http.createServer(function(request,response){
	response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});  
	//添加if语句是为了清楚第二次访问
	if(request.url!=="/favicon.ico"){
		console.log('request');
		response.write('hello world');
		//不写则没有http协议尾,但写了会产生两次访问
		response.end('');
	}
}).listen(3000);

console.log('Server running at http://127.0.0.1:3000/');  

按win+R打开运行命令,输入cmd。然后进入文件所在目录。输入

node n1_hello.js
如图所示:



然后打开浏览器,输入

http://localhost:3000/

出现


Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐