码迷,mamicode.com
首页 > Web开发 > 详细

node Express 创建一个web应用

时间:2015-08-16 18:27:08      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:node express 创建一个web   node express web应用   node express   


环境安装的node版本是最新的,下个月会发布4.0,把io.js合并进来。

[doctor@localhost myExpressApp]$ node --version
v0.12.7


npm和node版本中的npm版本更新是不同布的,我们要经常更新npm包,

[doctor@localhost ~]$ npm install --g npm
/home/doctor/opt/node/bin/npm -> /home/doctor/opt/node/lib/node_modules/npm/bin/npm-cli.js
npm@2.13.5 /home/doctor/opt/node/lib/node_modules/npm
[doctor@localhost ~]$ 

Express是hode中非常出名的重要web框架。我们可以用Express Generator工具生成一个Express应用。

安装Express Generator工具

[doctor@localhost ~]$ npm install -g express-generator
/home/doctor/opt/node/bin/express -> /home/doctor/opt/node/lib/node_modules/express-generator/bin/express
express-generator@4.13.1 /home/doctor/opt/node/lib/node_modules/express-generator
├── sorted-object@1.0.0
├── mkdirp@0.5.1 (minimist@0.0.8)
└── commander@2.7.1 (graceful-readlink@1.0.1)
[doctor@localhost ~]$ 

下面我们创建一个Express应用:

[doctor@localhost WebstormProjects]$ express myExpressApp

   create : myExpressApp
   create : myExpressApp/package.json
   create : myExpressApp/app.js
   create : myExpressApp/public
   create : myExpressApp/routes
   create : myExpressApp/routes/index.js
   create : myExpressApp/routes/users.js
   create : myExpressApp/views
   create : myExpressApp/views/index.jade
   create : myExpressApp/views/layout.jade
   create : myExpressApp/views/error.jade
   create : myExpressApp/bin
   create : myExpressApp/bin/www
   create : myExpressApp/public/javascripts
   create : myExpressApp/public/images
   create : myExpressApp/public/stylesheets
   create : myExpressApp/public/stylesheets/style.css

   install dependencies:
     $ cd myExpressApp && npm install

   run the app:
     $ DEBUG=myExpressApp:* npm start

根据提示:

安装应用依赖包:


[doctor@localhost WebstormProjects]$ cd myExpressApp/
[doctor@localhost myExpressApp]$ npm install
cookie-parser@1.3.5 node_modules/cookie-parser
├── cookie@0.1.3
└── cookie-signature@1.0.6

debug@2.2.0 node_modules/debug
└── ms@0.7.1

serve-favicon@2.3.0 node_modules/serve-favicon
├── fresh@0.3.0
├── etag@1.7.0
├── ms@0.7.1
└── parseurl@1.3.0

morgan@1.6.1 node_modules/morgan
├── basic-auth@1.0.3
├── on-headers@1.0.0
├── depd@1.0.1
└── on-finished@2.3.0 (ee-first@1.1.1)

body-parser@1.13.3 node_modules/body-parser
├── bytes@2.1.0
├── content-type@1.0.1
├── depd@1.0.1
├── qs@4.0.0
├── iconv-lite@0.4.11
├── http-errors@1.3.1 (inherits@2.0.1, statuses@1.2.1)
├── on-finished@2.3.0 (ee-first@1.1.1)
├── raw-body@2.1.2 (unpipe@1.0.0)
└── type-is@1.6.6 (media-typer@0.3.0, mime-types@2.1.4)

express@4.13.3 node_modules/express
├── escape-html@1.0.2
├── merge-descriptors@1.0.0
├── array-flatten@1.1.1
├── cookie@0.1.3
├── utils-merge@1.0.0
├── cookie-signature@1.0.6
├── methods@1.1.1
├── fresh@0.3.0
├── range-parser@1.0.2
├── vary@1.0.1
├── path-to-regexp@0.1.7
├── content-type@1.0.1
├── etag@1.7.0
├── parseurl@1.3.0
├── content-disposition@0.5.0
├── serve-static@1.10.0
├── depd@1.0.1
├── on-finished@2.3.0 (ee-first@1.1.1)
├── finalhandler@0.4.0 (unpipe@1.0.0)
├── qs@4.0.0
├── type-is@1.6.6 (media-typer@0.3.0, mime-types@2.1.4)
├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1)
├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.4)
└── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)

jade@1.11.0 node_modules/jade
├── character-parser@1.2.1
├── void-elements@2.0.1
├── commander@2.6.0
├── constantinople@3.0.2 (acorn@2.2.0)
├── with@4.0.3 (acorn@1.2.2, acorn-globals@1.0.5)
├── mkdirp@0.5.1 (minimist@0.0.8)
├── clean-css@3.3.9 (commander@2.8.1, source-map@0.4.4)
├── uglify-js@2.4.24 (uglify-to-browserify@1.0.2, async@0.2.10, source-map@0.1.34, yargs@3.5.4)
├── transformers@2.1.0 (css@1.0.8, uglify-js@2.2.5, promise@2.0.0)
└── jstransformer@0.0.2 (is-promise@2.0.0, promise@6.1.0)
[doctor@localhost myExpressApp]$ 

一个基本的Express应用建好了,运行一下:


[doctor@localhost myExpressApp]$ DEBUG=myExpressApp:* npm start

> myExpressApp@0.0.0 start /home/doctor/WebstormProjects/myExpressApp
> node ./bin/www

  myExpressApp:server Listening on port 3000 +0ms

或直接:
npm start
技术分享



如果让编辑器代码提示的更好,需要安装微软的 TypeScript Definition Manager (TSD)

npm install -g tsd

现在我们可以用tsd下载node和express的定义文件:

[doctor@localhost myExpressApp]$ tsd query node

 - node / node    
[doctor@localhost myExpressApp]$ tsd query node --action install

 - node / node    

>> running install..

>> written 1 file:

    - node/node.d.ts
[doctor@localhost myExpressApp]$ tsd query express

 - express / express    
[doctor@localhost myExpressApp]$ tsd query express --action install

 - express / express    

>> running install..

>> written 1 file:

    - express/express.d.ts
[doctor@localhost myExpressApp]$ 

写代码的离不开调式工具,它是一个最真诚的朋友,伴随你一生。

node express我用的是微软开源的vs code(不要喷微软,起码人家已经开始为开源做贡献了,不像有些公司拿了开源的就封闭了),要让vs code能调试node express ,我们需要安装mono。

我还是懒得下载rar包,解压,配置环境了,dnf在线安装吧。

安装文档在:http://www.mono-project.com/docs/getting-started/install/linux/#usage


 重要的事情重复一下,重要的事情重复一下:

CentOS, Fedora, and derivatives

Add the Mono Project GPG signing key and the package repository in a root shell with:

rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"
yum-config-manager --add-repo http://download.mono-project.com/repo/centos/

Run a package upgrade to upgrade existing packages to the latest available. Then install Mono as described in the Usage section.

Users of CentOS or RHEL (or similar distributions) may need to add the EPEL repository to their system to satisfy all dependencies.




[root@localhost ~]# rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"

[root@localhost ~]# 
[root@localhost ~]# yum-config-manager --add-repo http://download.mono-project.com/repo/centos/

Yum-utils package has been deprecated, use dnf instead.
See 'man yum2dnf' for more information.


Repository pgdg94 is listed more than once in the configuration
Repository pgdg94-source is listed more than once in the configuration
adding repo from: http://download.mono-project.com/repo/centos/

[download.mono-project.com_repo_centos_]
name=added from: http://download.mono-project.com/repo/centos/
baseurl=http://download.mono-project.com/repo/centos/
enabled=1


[root@localhost ~]# dnf install mono

设置一个断点:14行处:

技术分享


打开调试视图,左侧竖着放的菜单最后一个:

技术分享

断点处程序停止了。上面多出了一个可以控制调试流程的小工具。

版权声明:本文为博主原创文章,未经博主允许不得转载[http://blog.csdn.net/doctor_who2004]。

node Express 创建一个web应用

标签:node express 创建一个web   node express web应用   node express   

原文地址:http://blog.csdn.net/doctor_who2004/article/details/47702845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!