What is "express"

Installation

Assuming you’ve already installed Node.js, create a directory to contain your app, and make it your active directory.

mkdir myapp
cd myapp

Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Treatment details of npm package.json.

npm init

This command prompts for several things, such as the name and version of your application. For now, it is possible to simply press Enter to accept the standards for most of them, with the following exceptions:

entry point: (index.js)

Enter app.js, or any name you want for the main file. If you want it to be index.js, press Enter to accept the suggested default filename.

Now install Express in the app directory and save it in the dependencies list. For example:

npm install express --save

To install Express temporarily do not include it in the dependencies list, omit the option --save:

npm install express

An example Hello world/Hello world

In the myapp directory, create a file called app.js and include the following code:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.send('Olá mundo!');
});

app.listen(3000, function () {
    console.log('App escutando a porta 3000!');
});

Run the application with the following command:

node app.js

The application starts a server and listens to port 3000 by connections. The application responds with Olá mundo! to requests for the root URL (/) or route. For all other paths, it will respond with a 404 Not Found.

Then open in your browser the address http://localhost:3000 to test.

The req (requisition) and res (answer) in app.get('/', function (req, res) {, are the same objects as the Node provides, so that it is possible to call the req.pipe(), req.on('data', callback) and anything else I’d like to do without the involvement of Express.

More details on:

Express Application Generator

Use the express-Enerator app-generating tool to quickly create a basic app structure.

Install the express-Enerator with the following command:

npm install express-generator -g

Display command options with the option -h:

express -h

  Usage: express [options][dir]

  Options:

    -h, --help          output usage information
        --version       output the version number
    -e, --ejs           add ejs engine support
        --hbs           add handlebars engine support
        --pug           add pug engine support
    -H, --hogan         add hogan.js engine support
    -v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
    -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

For example, the following creates an Express application called myapp in the directory currently in operation:

express --view=pug 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.pug
   create : myapp/views/layout.pug
   create : myapp/views/error.pug
   create : myapp/bin
   create : myapp/bin/www

Then install the dependencies:

cd myapp
npm install

On macOS or Linux, run the application with this command:

DEBUG=myapp:* npm start

In Windows, use this command:

set DEBUG=myapp:* & npm start

Then navigate to the address http://localhost:3000/ in your browser to access the application.

The generated application has the following directory structure:

.
├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug

7 directories, 9 files