node简单配置一台服务器

要想使用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    //服务器响应

代码如下:

1 var http = require('http');    //调用node中http类
2 http.createServer(function(req,res){    //创建服务器
3     console.log("已连接!");    //服务器响应
4 }).listen(8808);    //设置端口号
5     

现在我们访问:http://127.0.0.1:8808   node控制台就会响应,简单的服务器搭建成功。

posted @ 2017-09-13 09:07  孙三峰  阅读(588)  评论(0编辑  收藏  举报