Express Helloworld for Mac OSX

Express Helloworld for Mac OS X

install node.js

  • download node.js from https://nodejs.org/en/ current version is v4.1.1 (2015/10/05)

  • install the package

    Node.js was installed at
    /usr/local/bin/node
    npm was installed at
    /usr/local/bin/npm
    Make sure that /usr/local/bin is in your $PATH.

  • try node command

    $ node -v
    v4.1.1
    $ node -h
    Usage: node [options] [ -e script | script.js ] [arguments]
    node debug script.js [arguments]

    $ npm -v
    2.14.4
    $ npm help

    $ npm -l

install express

  • install express

    $ sudo npm install -g express
    $ ls /usr/local/lib/nodemodules/express/
    History.md LICENSE Readme.md index.js lib node
    modules package.json

  • install express-generator

    $ sudo npm install -g express-generator
    $ ls /usr/local/lib/nodemodules/express-generator/
    LICENSE README.md bin node
    modules package.json templates

create express skeleton

  • use express create skeleton source code

    $ mkdir helloworld
    $ cd helloworld/
    $ express myapp
    create : myapp
    create : myapp/package.json
    create : myapp/app.js
    create : myapp/public
    create : myapp/public/javascripts
    create : myapp/public/images
    create : myapp/public/stylesheets
    create : myapp/public/stylesheets/style.css
    create : myapp/routes
    create : myapp/routes/index.js
    create : myapp/routes/users.js
    create : myapp/views
    create : myapp/views/index.jade
    create : myapp/views/layout.jade
    create : myapp/views/error.jade
    create : myapp/bin
    create : myapp/bin/www
    install dependencies:
    $ cd myapp && npm install
    run the app:
    $ DEBUG=myapp:* npm start

  • install require modules with package.json file under myapp $ cd myapp
    $ nmp install
    serve-favicon@2.3.0 nodemodules/serve-favicon
    ├── etag@1.7.0
    ├── fresh@0.3.0
    ├── ms@0.7.1
    └── parseurl@1.3.0
    cookie-parser@1.3.5 node
    modules/cookie-parser
    ├── cookie@0.1.3
    └── cookie-signature@1.0.6
    … …
    $ ls node_modules/
    body-parser cookie-parser debug express jade morgan serve-favicon

run express application

  • run myapp express application, and access http://127.0.0.1:3000/

    $ DEBUG=myapp:* npm start
    myapp@0.0.0 start express/helloworld/myapp
    node ./bin/www
    myapp:server Listening on port 3000 +0ms

    GET / 200 502.117 ms - 170
    GET /stylesheets/style.css 200 4.042 ms - 111
    GET /favicon.ico 404 27.750 ms - 1335

  • take a look at the index.js cgi source

    $ cat routes/index.js
    var express = require(‘express’);
    var router = express.Router();

    /* GET home page. */
    router.get(‘/’, function(req, res, next) {
    res.render(‘index’, { title: ‘Express’ });
    });

    module.exports = router;

  • and the template of index (jade template engine)

    $ cat views/index.jade
    extends layout

    block content
    h1= title
    p Welcome to #{title}



支 持 本 站: 捐赠服务器等运维费用,感谢您的支持!
支 持 本 站: 捐赠服务器等运维费用,感谢您的支持!

发布时间: