控制 tro 创建服务 方法 creates 端口 pre 为我
要想使用nodeJS来搭建服务器,首先需要一个必备的条件:node必须安装,建议为4.0版本及以上;
在node中,为我们封装了好多类,搭建服务器需要的一个类是“http”类。
用法:var http = require(‘http‘);这样http就会有http类中的所有方法。
http.createServer(function(req,res){
console.log("已连接服务器");
}).listen(8808);
createServer //创建服务器
listen//设置端口号
req//是服务器接收的参数
res//服务器响应
代码如下:
- var http = require(‘http‘); //调用node中http类
- http.createServer(function(req,res){ //创建服务器
- console.log("已连接!"); //服务器响应
- }).listen(8808); //设置端口号
现在我们访问:http://127.0.0.1:8808 node控制台就会响应,简单的服务器搭建成功。
node简单配置一台服务器
来源: http://www.bubuko.com/infodetail-2305339.html