美文网首页
Nodejs | 创建 Node.js 应用

Nodejs | 创建 Node.js 应用

作者: Ricsy | 来源:发表于2019-04-26 18:02 被阅读0次


    • 新建server.js
    //require 指令来载入 http 模块,并将实例化的 HTTP 赋值给变量 http
    var http = require('http');
    
    var port = 8888 ;
    var hostname = '127.0.0.1';
    
    // http.createServer() 方法创建服务器,并使用 listen 方法绑定 8888 端口。 函数通过 request, response 参数来接收和响应数据
    http.createServer(function (request, response) {
        
        // 发送 HTTP 头部 
        // HTTP 状态值: 200 : OK
        // 内容类型: text/plain
        response.writeHead(200, {'Content-Type': 'text/plain'});
    
        // 发送响应数据 "Hello World"
        response.end('Hello World\n');
    }).listen(port,hostname);
    
    // 终端打印如下信息
    console.log(`Server running at http://${hostname}:${port}/`);
    

    提示:

    • 用模板字符串的话不能用单双引号(" '),要用反引号(`).
    • 运行
      node srever.js

    更新中......


    相关文章

      网友评论

          本文标题:Nodejs | 创建 Node.js 应用

          本文链接:https://www.haomeiwen.com/subject/jtmqnqtx.html