目录

Nodejs 基础

目录

查看NodeJS 版本

1
node -v

创建一个Http服务器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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/');

使用npm

在项目根目录下运行npm init创建package.json

1
npm init