Nodejs入门实战

来源:互联网 发布:医疗器械软件关税 编辑:程序博客网 时间:2024/05/21 20:30

1. 安装node express

hanruikaideMacBook-Pro:nodejs hanruikai$ mkdir myapp

hanruikaideMacBook-Pro:nodejs hanruikai$ cd myapp/

hanruikaideMacBook-Pro:myapp hanruikai$ npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.


See `npm help json` for definitive documentation on these fields

and exactly what they do.


Use `npm install <pkg>` afterwards to install a package and

save it as a dependency in the package.json file.


Press ^C at any time to quit.

package name: (myapp) 

version: (1.0.0) 

description: 

entry point: (index.js) 

test command: 

git repository: 

keywords: 

author: 

license: (ISC) 

About to write to /Users/hanruikai/eclipse-workspace/nodejs/myapp/package.json:


{

  "name": "myapp",

  "version": "1.0.0",

  "description": "",

  "main": "index.js",

  "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1"

  },

  "author": "",

  "license": "ISC"

}



Is this ok? (yes) 

hanruikaideMacBook-Pro:myapp hanruikai$ 


hanruikaideMacBook-Pro:myapp hanruikai$ su

Password:

sh-3.2# whoami

root

sh-3.2# npm install -g express-generator

/usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express-cli.js

+ express-generator@4.15.5

added 6 packages in 6.409s


2. 创建工程


sh-3.2# express  -e  nodejs-demo


  warning: option `--ejs' has been renamed to `--view=ejs'



   create : nodejs-demo

   create : nodejs-demo/package.json

   create : nodejs-demo/app.js

   create : nodejs-demo/public

   create : nodejs-demo/routes

   create : nodejs-demo/routes/index.js

   create : nodejs-demo/routes/users.js

   create : nodejs-demo/views

   create : nodejs-demo/views/index.ejs

   create : nodejs-demo/views/error.ejs

   create : nodejs-demo/bin

   create : nodejs-demo/bin/www

   create : nodejs-demo/public/javascripts

   create : nodejs-demo/public/images

   create : nodejs-demo/public/stylesheets

   create : nodejs-demo/public/stylesheets/style.css


   install dependencies:

     $ cd nodejs-demo && npm install


   run the app:

     $ DEBUG=nodejs-demo:* npm start