Node.js + Express.js 环境配置

来源:互联网 发布:windows系统字体大小 编辑:程序博客网 时间:2024/06/04 19:25

前言

Node.js 如今越来越火,不管你走到哪里,跟Node.js 打交道的机会还是很多的,例如:当你想换工作的时候,在各大求职网站上东找找西找找的时候,你肯定会看到 会Node.js 或者 精通 Node.js。一直都觉得 Node.js 好高大尚,所以也不敢撞他,但不管我如何对它,都不会阻止它的流行及在前端中的地位,所以今天我还是硬着头皮开始了Node.js 之旅。我这次是在Window 系统下安装测试的,但是万变不离其踪,以不变变万变,Linux、Mac 也都差不多。

环境配置

工欲善其事必先利其器,所以我们在学习 Node.js 之前,先把这 Node.js 的环境配置好。这里也不多说什么了,先直接贴出配置的整个过程

  1. Microsoft Windows [版本 6.1.7601]
  2. 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
  3. C:\Users\myPC>npm install -g express-generator
  4. C:\Users\myPC\AppData\Roaming\npm\express -> C:\Users\myPC\AppData
  5. \Roaming\npm\node_modules\express-generator\bin\express
  6. C:\Users\myPC\AppData\Roaming\npm
  7. `-- express-generator@4.13.4
  8. +-- commander@2.7.1
  9. | `-- graceful-readlink@1.0.1
  10. +-- mkdirp@0.5.1
  11. | `-- minimist@0.0.8
  12. `-- sorted-object@2.0.0
  13. C:\Users\myPC>express -e blog
  14. create : blog
  15. create : blog/package.json
  16. create : blog/app.js
  17. create : blog/routes
  18. create : blog/routes/index.js
  19. create : blog/routes/users.js
  20. create : blog/public
  21. create : blog/views
  22. create : blog/views/index.ejs
  23. create : blog/views/error.ejs
  24. create : blog/bin
  25. create : blog/bin/www
  26. create : blog/public/stylesheets
  27. create : blog/public/stylesheets/style.css
  28. install dependencies:
  29. > cd blog && npm install
  30. run the app:
  31. > SET DEBUG=blog:* & npm start
  32. create : blog/public/images
  33. create : blog/public/javascripts
  34. C:\Users\myPC>cd blog &npm install
  35. blog@0.0.0 C:\Users\myPC\blog
  36. +-- body-parser@1.15.2
  37. | +-- bytes@2.4.0
  38. | +-- content-type@1.0.2
  39. | +-- depd@1.1.0
  40. | +-- http-errors@1.5.0
  41. | | +-- inherits@2.0.1
  42. | | +-- setprototypeof@1.0.1
  43. | | `-- statuses@1.3.0
  44. | +-- iconv-lite@0.4.13
  45. | +-- on-finished@2.3.0
  46. | | `-- ee-first@1.1.1
  47. | +-- qs@6.2.0
  48. | +-- raw-body@2.1.7
  49. | | `-- unpipe@1.0.0
  50. | `-- type-is@1.6.13
  51. | +-- media-typer@0.3.0
  52. | `-- mime-types@2.1.11
  53. | `-- mime-db@1.23.0
  54. +-- cookie-parser@1.4.3
  55. | +-- cookie@0.3.1
  56. | `-- cookie-signature@1.0.6
  57. +-- debug@2.2.0
  58. | `-- ms@0.7.1
  59. +-- ejs@2.4.2
  60. +-- express@4.13.4
  61. | +-- accepts@1.2.13
  62. | | `-- negotiator@0.5.3
  63. | +-- array-flatten@1.1.1
  64. | +-- content-disposition@0.5.1
  65. | +-- cookie@0.1.5
  66. | +-- escape-html@1.0.3
  67. | +-- etag@1.7.0
  68. | +-- finalhandler@0.4.1
  69. | +-- fresh@0.3.0
  70. | +-- merge-descriptors@1.0.1
  71. | +-- methods@1.1.2
  72. | +-- parseurl@1.3.1
  73. | +-- path-to-regexp@0.1.7
  74. | +-- proxy-addr@1.0.10
  75. | | +-- forwarded@0.1.0
  76. | | `-- ipaddr.js@1.0.5
  77. | +-- qs@4.0.0
  78. | +-- range-parser@1.0.3
  79. | +-- send@0.13.1
  80. | | +-- destroy@1.0.4
  81. | | +-- http-errors@1.3.1
  82. | | +-- mime@1.3.4
  83. | | `-- statuses@1.2.1
  84. | +-- serve-static@1.10.3
  85. | | `-- send@0.13.2
  86. | | +-- http-errors@1.3.1
  87. | | `-- statuses@1.2.1
  88. | +-- utils-merge@1.0.0
  89. | `-- vary@1.0.1
  90. +-- morgan@1.7.0
  91. | +-- basic-auth@1.0.4
  92. | `-- on-headers@1.0.1
  93. `-- serve-favicon@2.3.0
  94. C:\Users\myPC\blog>node -v
  95. v6.3.1
  96. C:\Users\myPC\blog>npm -v
  97. 3.10.3
  98. C:\Users\myPC\blog>SET DEBUG=blog:* & npm start
  99. > blog@0.0.0 start C:\Users\myPC\blog
  100. > node ./bin/www
  101. blog:server Listening on port 3000 +0ms
  102. GET / 200 21.915 ms - 207
  103. GET /stylesheets/style.css 200 4.449 ms - 111
  104. GET /favicon.ico 404 5.986 ms - 946
  105. GET / 304 3.439 ms - -
  106. GET /stylesheets/style.css 304 0.949 ms - -

不要被这么一大堆代码吓到了,其实需要你输入的命令行也就那么几个句,你可看好了,下面我就把它都一个一个揪出来。

安装Express

什么是 Express ? Express 是Node.js 上最流行的 Web 开发框架,我们可以通过它来开发一个 Web 应用。

  1. npm install -g express-generator

创建项目

创建一个 blog 文件夹

  1. express -e blog

命令行中的 -e 表示 ejs 模板引擎,不写 -e  默认的创建jade模板引擎

初始化一个Express 项目并安装所需要模块。

  1. cd blog && npm install

cd blog //进入到你创建的项目目录
npm install //读取根目录中的package.json文件然后安装项目所依赖的包

启用项目

接下来我们要做的就是开启这个项目,让他成为可访问的站点。

  1. SET DEBUG=blog:* & npm start

当你看到这个提示时说明你已经成功了

  1. blog:server Listening on port 3000 +0ms

接下来就是见证奇迹的时刻了,在浏览器中输入localhost:3000,回车后你就可以看到一个简单而充满成就感页面了。

node.js express

现在,你可以开始你的建站之旅了,预祝你一路顺风,过五关斩六将,在Node.js的征程之路越走越好。

后继配置

细节说明

可当你重启电脑后,你会发现 localhost:3000无法访问了。这是什么问题呢?根据自己多年的临床经验,我觉得应该是没有启用某个服务。于是我就回想了下在安装的时候启用了什么服务,不用多想就只有一个那就是它:

  1. npm start

win+R 键打终端,运行上面的启动命令行,但失败了出现如下提示

C:\Users\myPC>npm start
npm ERR! Windows_NT 6.1.7601
npm ERR! argv “D:\\Program Files\\nodejs\\node.exe” “D:\\Program Files\\nodejs\\
node_modules\\npm\\bin\\npm-cli.js” “start”
npm ERR! node v6.3.1
npm ERR! npm v3.10.3
npm ERR! path C:\Users\myPC\package.json
npm ERR! code ENOENT
npm ERR! errno -4058
npm ERR! syscall open

npm ERR! enoent ENOENT: no such file or directory, open ‘C:\Users\myPC\pa
ckage.json’
npm ERR! enoent ENOENT: no such file or directory, open ‘C:\Users\myPC\pa
ckage.json’
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! Please include the following file with any support request:
npm ERR! C:\Users\myPC\npm-debug.log

怎么会这样?原因也很简单,原来我没有切换到站点目录blog,接着再启动就可以了。运行如下一行命令

  1. C:\Users\myPC>cd /blog
  2. C:\Users\myPC\blog>npm start

然后就会出现如下成功启动的信息提示了。你也可以用这行命令来启用SET DEBUG=blog:* & npm start

  1. > blog@0.0.0 start C:\Users\myPC\blog
  2. > node ./bin/www
  3. blog:server Listening on port 3000 +0ms
  4. GET / 200 75.146 ms - 207
  5. GET /stylesheets/style.css 304 4.382 ms - -
  6. GET /favicon.ico 404 5.675 ms - 946

supervisor 配置

还有一个不大不小的问题,那就是当我们修改了模板代码时,都必需手动中止并重启应用,这还真够麻烦的,但我们可以使用supervisor 模块来解决这个问题,安装完这个模块后,我们更改了模板也无需手动中止并重启应用,supervisor 会自动帮我们完成这两个步骤。现在你要做的就是执行如下命令行,安装supervisor模块即可。

  1. D:\Program Files\mongodb\yunkus>npm install -g supervisor

回车后你会看到这样的提示

D:\Program Files\mongodb\yunkus>npm install -g supervisor
C:\Users\myPC\AppData\Roaming\npm\supervisor -> C:\Users\myPC\AppD
ata\Roaming\npm\node_modules\supervisor\lib\cli-wrapper.js
C:\Users\myPC\AppData\Roaming\npm\node-supervisor -> C:\Users\myPC
\AppData\Roaming\npm\node_modules\supervisor\lib\cli-wrapper.js
C:\Users\myPC\AppData\Roaming\npm
`– supervisor@0.11.0

然后再执行命令行 supervisor ./bin/www 就可以了

  1. D:\Program Files\mongodb\yunkus>supervisor ./bin/www
  2. Running node-supervisor with
  3. program './bin/www'
  4. --watch '.'
  5. --extensions 'node,js,/bin/www'
  6. --exec 'node'
  7. Starting child process with 'node ./bin/www'
  8. Watching directory 'D:\Program Files\mongodb\yunkus' for changes.
  9. Press rs for restarting the process.
  10. yunkus:server Listening on port 3000 +0ms

现在你再试试修改模板,是不是改完后,你只需刷新下页面就可以看到更改后的效果了呀!

原创粉丝点击