要想使用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控制台就会响应,简单的服务器搭建成功。

相关文章:

  • 2021-07-15
  • 2021-06-23
  • 2021-12-05
  • 2021-12-07
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2021-04-01
猜你喜欢
  • 2021-07-18
  • 2021-10-16
  • 2022-02-07
  • 2021-12-05
  • 2021-11-28
  • 2022-01-31
相关资源
相似解决方案