您的位置:首页 > Web前端 > Node.js

node.jsの安装helloWorld

2017-01-16 13:31 369 查看

下载

下载地址:https://nodejs.org/en/


安装

测试

dos下:npm

查看npm版本 npm -v

查看node.js版本 node -v

简单的helloWorld

新建hello.js文件,
console.log("hello,world");
右击--打开dos窗口
node hello.js





以上是最简单的helloworld

交互的helloWorld

因为node.js主要是和服务端的交互,下面代码是完成的一次和服务端交互的node.js

var http=require('http');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
if(request.url!="/favicon.ico"){//清楚第二次次访问
console.log('访问');
response.write('hello,world');//http请求内容
response.end('结束了');//请求结束,否则浏览器会一直转

}

}).listen(8000);
console.log('服务运行在本地8000端口');





代码步骤总结:

1.获取http对象
2.创建服务

2.1请求,和反应

2.2写协议头

...写内容...

2.3写协议尾
3.监听8000端口

下一次总结学习node.js的函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息