初步接触node.js

  1. 首先搭建nodejs的环境
    http://nodejs.cn/download/ 挑选个自己系统的安装软件,
  2. 跑出Hello word
    创建个文件,内容填入
    var http = require('http');
    
    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(8888);
    
    // 终端打印如下信息
    console.log('Server running at http://127.0.0.1:8888/');
    保存为后缀为.js文件

    引入 required 模块:var http = require('http'); (我们可以使用 require 指令来载入 Node.js 模块。)
    创建服务器: http.createServer (服务器可以监听客户端的请求,类似于 Apache 、Nginx 等 HTTP 服务器。)
    接收请求与响应请求 服务器很容易创建,客户端可以使用浏览器或终端发送 HTTP 请求,服务器接收请求后返回响应数据。
  3. 执行命令
    node server.js
    打开浏览器访问 http://127.0.0.1:8888/,你会看到一个写着 "Hello World"的网页。

    尼玛,真特么快

猜你喜欢

转载自my.oschina.net/u/3491516/blog/1547979
今日推荐