环境安装的node版本是最新的,下个月会发布4.0,把io.js合并进来。
[[email protected] myExpressApp]$ node --version v0.12.7
npm和node版本中的npm版本更新是不同布的,我们要经常更新npm包,
[[email protected] ~]$ npm install --g npm /home/doctor/opt/node/bin/npm -> /home/doctor/opt/node/lib/node_modules/npm/bin/npm-cli.js [email protected] /home/doctor/opt/node/lib/node_modules/npm [[email protected] ~]$
Express是hode中非常出名的重要web框架。我们可以用Express Generator工具生成一个Express应用。
安装Express Generator工具:
[[email protected] ~]$ npm install -g express-generator /home/doctor/opt/node/bin/express -> /home/doctor/opt/node/lib/node_modules/express-generator/bin/express [email protected] /home/doctor/opt/node/lib/node_modules/express-generator ├── [email protected] ├── [email protected] ([email protected]) └── [email protected] ([email protected]) [[email protected] ~]$
下面我们创建一个Express应用:
[[email protected] 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
根据提示:
安装应用依赖包:
[[email protected] WebstormProjects]$ cd myExpressApp/ [[email protected] myExpressApp]$ npm install [email protected] node_modules/cookie-parser ├── [email protected] └── [email protected] [email protected] node_modules/debug └── [email protected] [email protected] node_modules/serve-favicon ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected] [email protected] node_modules/morgan ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected] ([email protected]) [email protected] node_modules/body-parser ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected]) ├── [email protected] ([email protected]) └── [email protected] ([email protected], [email protected]) [email protected] node_modules/express ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ([email protected]) ├── [email protected] ([email protected]) ├── [email protected] ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected], [email protected]) └── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected]) [email protected] node_modules/jade ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ([email protected]) ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected]) ├── [email protected] ([email protected], [email protected]) ├── [email protected] ([email protected], [email protected], [email protected], [email protected]) ├── [email protected] ([email protected], [email protected], [email protected]) └── [email protected] ([email protected], [email protected]) [[email protected] myExpressApp]$
一个基本的Express应用建好了,运行一下:
[[email protected] myExpressApp]$ DEBUG=myExpressApp:* npm start > [email protected] 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的定义文件:
[[email protected] myExpressApp]$ tsd query node - node / node [[email protected] myExpressApp]$ tsd query node --action install - node / node >> running install.. >> written 1 file: - node/node.d.ts [[email protected] myExpressApp]$ tsd query express - express / express [[email protected] myExpressApp]$ tsd query express --action install - express / express >> running install.. >> written 1 file: - express/express.d.ts [[email protected] 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.
[[email protected] ~]# rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" [[email protected] ~]# [[email protected] ~]# 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 [[email protected] ~]# dnf install mono
设置一个断点:14行处:
打开调试视图,左侧竖着放的菜单最后一个:
断点处程序停止了。上面多出了一个可以控制调试流程的小工具。
版权声明:本文为博主原创文章,未经博主允许不得转载[http://blog.csdn.net/doctor_who2004]。