express installation
npm install express -g
//Initialize project
node initÂ
//Install express
npm install express
After that, create a new app.js file with the following contents
//Launch projectvar express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
node app.js
Open the browser and input localhost:3000. Our first hello world is finished!
You can quickly create an application skeleton through the application builder tool express.
npm install express-generator -g
//View all express commands
$ express -h Usage: express [options] [dir] Options: -h, --help output usage information -V, --version output the version number -e, --ejs add ejs engine support (defaults to jade) --hbs add handlebars engine support -H, --hogan add hogan.js engine support -c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css) --git add .gitignore -f, --force force on non-empty directory
Create a myapp project
//Install all dependencies$ 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/routes create : myapp/routes/index.js create : myapp/routes/users.js create : myapp/public/stylesheets create : myapp/public/stylesheets/style.css 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
npm install
Start this application (MAC OS or Linux platform):
$ DEBUG=myapp npm start
The Windows platform uses the following commands:
After the browser opens localhost:3000, the express project is successfully started> set DEBUG=myapp & npm start