- 安裝node
本文以安裝node_v8.9.0為例(win10環境),下載node-v8.9.0-x64.msi插件
下載后,安裝,安裝目錄默認為C:\Program Files\nodejs
配置環境變量,系統變量->path,添加“C:\Program Files\nodejs\”
運行cmd,輸入node -v
C:\Windows\system32>node -v v8.9.0
- 安裝express
找到node安裝目錄C:\Program Files\nodejs,命令行運行
C:\Program Files\nodejs> npm install express
運行結果
npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning ENOENT: request to https://registry.npmjs.org/express failed, reason: getaddrinfo ENOENT registry.npmjs.org:443 npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation. npm WARN enoent ENOENT: no such file or directory, open 'C:\Program Files\nodejs\package.json'e_modules\libnpx npm WARN nodejs No description npm WARN nodejs No repository field. npm WARN nodejs No README data npm WARN nodejs No license field. npm ERR! path C:\Program Files\nodejs\node_modules\npm\node_modules\bytes npm ERR! code ENOENT npm ERR! errno -4058 npm ERR! syscall rename npm ERR! enoent ENOENT: no such file or directory, rename 'C:\Program Files\nodejs\node_modules\npm\node_modules\bytes' -> 'C:\Program Files\nodejs\node_modules\bytes' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR!
npm install express失敗可能導致nodejs/node_modules文件夾被清空,導致后續的npm install操作會失敗,通過重新安裝node-v8.9.0-x64.msi修復即可。
上面操作失敗是因為找不到相關的文件導致,需要更改到npm文件夾目錄下去操作
> cd C:\Program Files\nodejs\node_modules\npm > npm install express
還是報錯,查找了下原因,當前電腦用戶沒有修改下面這個文件夾的權限(C:\Program Files\nodejs\node_modules\npm\node_modules),
install的express是安裝在這個目錄下,網上提供的解決方法是用管理員的身份運行命令,
(win10環境)筆者是直接右擊文件夾,屬性->安全,組或用戶那里選擇當前電腦用戶,
然后權限把“修改”設置為允許。接着重新運行npm install express就可以正常安裝
運行結果
npm notice created a lockfile as package-lock.json. You should commit this file. + express@4.16.3 added 47 packages in 125.339s
安裝express-generator
> npm install express-generator
運行結果
+ express-generator@4.16.0 added 7 packages in 6.866s
安裝完成后,查看版本號
C:\Windows\system32>express -V 'express' 不是內部或外部命令,也不是可運行的程序 或批處理文件。
在網上查了一部分資料,解決這個問題的關鍵點在於找到express.cmd被安裝的位置
不同版本安裝的位置可能有所不同,比如有的會在(C:\Program Files\nodejs\node_modules\.bin),
有的是被直接安裝在node_modules文件夾下;
本人電腦是被安裝位置到C:\Program Files\nodejs\node_modules\npm\node_modules\.bin
配置express環境變量
系統變量->path,添加C:\Program Files\nodejs\node_modules\npm\node_modules\.bin
配置完成后,執行查詢版本命令
C:\Windows\system32>express -V
語法錯誤,修改成express --version
C:\Windows\system32>express --version 4.16.0
到這里,node和express安裝配置完成
- 新建項目
打開要新建項目的文件目錄,新建項目myApp
> cd node_workspace/debugDemo > express myApp
運行結果:
warning: the default view engine will not be jade in future releases warning: use `--view=jade' or `--help' for additional options create : myApp\ create : myApp\public\ create : myApp\public\javascripts\ create : myApp\public\images\ create : myApp\public\stylesheets\ create : myApp\public\stylesheets\style.css create : myApp\routes\ create : myApp\routes\index.js create : myApp\routes\users.js create : myApp\views\ create : myApp\views\error.jade create : myApp\views\index.jade create : myApp\views\layout.jade create : myApp\app.js create : myApp\package.json create : myApp\bin\ create : myApp\bin\www change directory: > cd myApp install dependencies: > npm install run the app: > SET DEBUG=myapp:* & npm start
在文件目錄下可以找到新建的myApp項目
在myApp目錄下新建hello.js
var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); console.log("nodejs start listen 8888 port!");
然后在項目目錄下,運行node hello.js
E:\node_workspace\debugDemo\myApp>node hello.js nodejs start listen 8888 port!
在瀏覽器輸入http://127.0.0.1:8888/
頁面顯示 Hello World
--本篇到此結束--