← 全部文章

node.js开篇

文章目录

Node.js 标志

下载与安装

下载地址:http://nodejs.org/#download

解压后执行:

./configure
make
make install

如果遇到权限错误,可以尝试使用 sudo。

Hello World

创建示例目录与文件:

mkdir demo
cd demo
vim example.js

在 example.js 中写入:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

运行:

node example.js

输出:

Server running at http://127.0.0.1:1337/

参考