- 官网下载msi版的安装文件,双击运行,直到安装完毕。
- 这里默认的安装路径在C:/Program Files/nodejs,也自动安装了NPM(nodejs 包管理器),已经自动加入系统路径path
- 要测试是否安装成功,在cmd下node -v,显示版本信息。注意,这里的命令行要在管理员模式下运行才行
- 通过加载一个js文件试运行一下,比如下面d:/index.js:
<pre name="code" class="html">/**
第一节:开篇章
*小试牛刀
**/
//创建http对象
var http = require('http');
//创建服务端
http.createServer(function (request, response) {
//发送一个HTTP状态200和HTTP头的内容类型(content-type),
response.writeHead(200, {'Content-Type': 'text/plain'});
//向HTTP相应主体中发送文本,完成响应
response.end('Hello World\n');
//输出日志信息
console.log('Server running function');
}).listen(8888);
//输出日志信息
console.log('Server running at http://127.0.0.1:8888/');
/*
第二种方法:
以下方法等同于上面方法
var http = require('http');
function onRequest(request, response){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end('Hello World\n');
console.log('Server running function onSer');
}
http.createServer(onRequest).listen('8888');
console.log('Server running at http://127.0.0.1:8888/');
*/
- 在cmd下命令node d:/index.js,会显示srever running....的信息,浏览器访问,成功