win7下安装nodejs环境

1、官网下载安装文件

  Node.js安装包及源码下载地址为:https://nodejs.org/en/download/。

  我安装到的D盘下,问题不大。

找了段测试代码

 1 var http = require('http');
 2 
 3 http.createServer(function (request, response) {
 4 
 5     // 发送 HTTP 头部 
 6     // HTTP 状态值: 200 : OK
 7     // 内容类型: text/plain
 8     response.writeHead(200, {'Content-Type': 'text/plain'});
 9 
10     // 发送响应数据 "Hello World"
11     response.end('Hello World\n');
12 }).listen(8888);
13 
14 // 终端打印如下信息
15 console.log('Server running at http://127.0.0.1:8888/');
server.js

命令行下执行:

node server.js

Server running at http://127.0.0.1:8888/

打开浏览器访问 http://127.0.0.1:8888/,就能看到写着“Hello World"的网页。

2、安装express模块

  命令行下执行:npm install -g express

执行上述命令后看到express已经安装,但是,在执行引用express模块的代码时,却提示“Cannot findmodule ‘express’ ",重新执行:npm install express --save,重新执行程序OK。

  这里npm的参数都是"--",不知道为什么这么特立独行。

 1 // 引入 express 模块
 2 var express = require('express');
 3 
 4 // 创建 express 实例
 5 var app = express();
 6 
 7 // 响应HTTP的GET方法
 8 app.get('/', function (req, res) {
 9   res.send('Hello World!');
10 });
11 
12 // 监听到8000端口
13 app.listen(8000, function () {
14   console.log('Hello World is listening at port 8000');
15 });
hello_express.js

暂时进行到这里吧。

猜你喜欢

转载自www.cnblogs.com/mofei004/p/9010564.html