express

安装express生成器

  1. npm i -g express-generator

说明:

  • npm是node package management
  • install可简写i,是安装的意思
  • global可简写-g,是全局安装的意思

安装之后

  1. workspace git:(i5ting) npm i -g express-generator
  2. /home/coding/.nvm/versions/node/v4.2.2/bin/express -> /home/coding/.nvm/versions/node/v4.2.2/lib/node_modules/express-generator/bin/express
  3. express-generator@4.13.1 /home/coding/.nvm/versions/node/v4.2.2/lib/node_modules/express-generator
  4. ├── sorted-object@1.0.0
  5. ├── commander@2.7.1 (graceful-readlink@1.0.1)
  6. └── mkdirp@0.5.1 (minimist@0.0.8)

可以看出/home/coding/.nvm/versions/node/v4.2.2/bin/express是/home/coding/.nvm/versions/node/v4.2.2/lib/node_modules/express-generator/bin/express的软连接

另外/home/coding/.nvm/versions/node/v4.2.2/bin是在环境变量里的,也就是说express这个命令可以直接访问

初始化express项目

  1. express demo
  2. create : demo
  3. create : demo/package.json
  4. create : demo/app.js
  5. create : demo/public
  6. create : demo/routes
  7. create : demo/routes/index.js
  8. create : demo/routes/users.js
  9. create : demo/public/javascripts
  10. create : demo/public/stylesheets
  11. create : demo/public/stylesheets/style.css
  12. create : demo/views
  13. create : demo/views/index.jade
  14. create : demo/views/layout.jade
  15. create : demo/views/error.jade
  16. create : demo/public/images
  17. create : demo/bin
  18. create : demo/bin/www
  19. install dependencies:
  20. $ cd demo && npm install
  21. run the app:
  22. $ DEBUG=demo:* npm start

然后安装依赖模块

  1. test cd demo
  2. demo ls
  3. app.js bin package.json public routes views
  4. demo npm i
  5. debug@2.2.0 node_modules/debug
  6. └── ms@0.7.1
  7. cookie-parser@1.3.5 node_modules/cookie-parser
  8. ├── cookie@0.1.3
  9. └── cookie-signature@1.0.6
  10. morgan@1.6.1 node_modules/morgan
  11. ├── on-headers@1.0.1
  12. ├── basic-auth@1.0.3
  13. ├── depd@1.0.1
  14. └── on-finished@2.3.0 (ee-first@1.1.1)
  15. serve-favicon@2.3.0 node_modules/serve-favicon
  16. ├── fresh@0.3.0
  17. ├── ms@0.7.1
  18. ├── etag@1.7.0
  19. └── parseurl@1.3.0
  20. body-parser@1.13.3 node_modules/body-parser
  21. ├── bytes@2.1.0
  22. ├── content-type@1.0.1
  23. ├── depd@1.0.1
  24. ├── on-finished@2.3.0 (ee-first@1.1.1)
  25. ├── qs@4.0.0
  26. ├── iconv-lite@0.4.11
  27. ├── http-errors@1.3.1 (statuses@1.2.1, inherits@2.0.1)
  28. ├── raw-body@2.1.4 (unpipe@1.0.0, iconv-lite@0.4.12)
  29. └── type-is@1.6.9 (media-typer@0.3.0, mime-types@2.1.7)
  30. express@4.13.3 node_modules/express
  31. ├── merge-descriptors@1.0.0
  32. ├── array-flatten@1.1.1
  33. ├── cookie@0.1.3
  34. ├── utils-merge@1.0.0
  35. ├── cookie-signature@1.0.6
  36. ├── methods@1.1.1
  37. ├── fresh@0.3.0
  38. ├── range-parser@1.0.3
  39. ├── escape-html@1.0.2
  40. ├── vary@1.0.1
  41. ├── path-to-regexp@0.1.7
  42. ├── content-type@1.0.1
  43. ├── etag@1.7.0
  44. ├── parseurl@1.3.0
  45. ├── serve-static@1.10.0
  46. ├── content-disposition@0.5.0
  47. ├── depd@1.0.1
  48. ├── finalhandler@0.4.0 (unpipe@1.0.0)
  49. ├── on-finished@2.3.0 (ee-first@1.1.1)
  50. ├── qs@4.0.0
  51. ├── type-is@1.6.9 (media-typer@0.3.0, mime-types@2.1.7)
  52. ├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1)
  53. ├── accepts@1.2.13 (negotiator@0.5.3, mime-types@2.1.7)
  54. └── send@0.13.0 (destroy@1.0.3, ms@0.7.1, statuses@1.2.1, mime@1.3.4, http-errors@1.3.1)
  55. jade@1.11.0 node_modules/jade
  56. ├── character-parser@1.2.1
  57. ├── void-elements@2.0.1
  58. ├── commander@2.6.0
  59. ├── constantinople@3.0.2 (acorn@2.6.4)
  60. ├── mkdirp@0.5.1 (minimist@0.0.8)
  61. ├── with@4.0.3 (acorn-globals@1.0.9, acorn@1.2.2)
  62. ├── jstransformer@0.0.2 (is-promise@2.1.0, promise@6.1.0)
  63. ├── clean-css@3.4.8 (source-map@0.4.4, commander@2.8.1)
  64. ├── transformers@2.1.0 (promise@2.0.0, css@1.0.8, uglify-js@2.2.5)
  65. └── uglify-js@2.6.1 (uglify-to-browserify@1.0.2, async@0.2.10, source-map@0.5.3, yargs@3.10.0)

然后启动服务器

  1. npm start

如果没有报错,就是启动成功了。

此时还无法测试,需要设置Access URL(访问地址)

express - 图1

说明:

  • 1)设置Port = 3000(因为bin/www里默认使用的是3000端口)
  • 2)点击Generate生成具体url
  • 3)点击下面的url地址,此时就会在浏览器里打开