express

Getting started with express

Remarks#

Express.js is, in the words of the developers, a “fast, unopinionated, minimalist web framework for Node.js.”

Designed to be minimal and flexible, Express offers a set of features for creating web and mobile applications. From HTTP methods to built-in middleware, Express is designed to provide you with the features you need to build a web or mobile app on Node.js.

If you want to build an app on Node.js Express is a great choice, whether you use vanilla Express or one of the many frameworks either based on Express or built on top of Express. A few of these frameworks can be found here.

Versions#

Versions from here.

VersionNotesRelease Date
4.15.32017-05-16
4.15.22017-03-06
4.15.12017-03-05
4.15.02017-03-01
4.14.12017-01-28
4.14.02016-06-16
4.13.42016-01-21
4.13.32015-08-02
4.13.22015-07-31
4.13.12015-07-05
4.13.02015-06-20
4.12.42015-05-17
4.12.32015-03-17
4.12.22015-03-02
4.12.12015-03-01
4.12.02015-02-23
4.11.22015-01-20
4.11.12015-01-20
4.11.02015-01-13
4.10.82015-01-13
4.10.72015-01-04
4.10.62014-12-12
4.10.52014-12-10
4.10.42014-11-24
4.10.32014-11-23
4.10.22014-11-09
4.10.12014-10-28
4.10.02014-10-23
4.9.82014-10-17
4.9.72014-10-10
4.9.62014-10-08
4.9.52014-09-24
4.9.42014-09-19
4.9.32014-09-18
4.9.22014-09-17
4.9.12014-09-16
4.9.02014-09-08
4.8.82014-09-04
4.8.72014-08-29
4.8.62014-08-27
4.8.52014-08-18
4.8.42014-08-14
4.8.32014-08-10
4.8.22014-08-07
4.8.12014-08-06
4.8.02014-08-05
4.7.42014-08-04
4.7.32014-08-04
4.7.22014-07-27
4.7.12014-07-26
4.7.02014-07-25
4.6.12014-07-12
4.6.02014-07-11
4.5.12014-07-06
4.5.02014-07-04
4.4.52014-06-26
4.4.42014-06-20
4.4.32014-06-11
4.4.22014-06-09
4.4.12014-06-02
4.4.02014-05-30
4.3.22014-05-28
4.3.12014-05-23
4.3.02014-05-21
4.2.02014-05-11
4.1.22014-05-08
4.1.12014-04-27
4.1.02014-04-24
4.0.02014-04-09
3.21.2From here to2015-07-31
3.21.13.18.6 dates2015-07-05
3.21.0seem wrong2015-06-18
3.20.32015-05-17
3.20.22015-03-16
3.20.12015-02-28
3.20.02015-02-18
3.19.22015-02-01
3.19.12015-01-20
3.19.02015-01-09
3.18.62014-12-12
3.18.52014-12-11
3.18.42014-11-23
3.18.32014-11-09
3.18.22014-10-28
3.18.12014-10-22
3.18.02014-10-17
3.17.82014-10-15
3.17.72014-10-08
3.17.62014-10-02
3.17.52014-09-24
3.17.42014-09-19
3.17.32014-09-18
3.17.22014-09-15
3.17.12014-09-08
3.17.02014-09-08
3.16.102014-09-04
3.16.92014-08-29
3.16.82014-08-27
3.16.72014-08-18
3.16.62014-08-14
3.16.52014-08-11
3.16.42014-08-10
3.16.32014-08-07
3.16.22014-08-07
3.16.12014-08-06
3.16.02014-08-05
3.15.32014-08-04
3.15.22014-07-27
3.15.12014-07-26
3.15.02014-07-22
3.14.02014-07-11
3.13.02014-07-03
3.12.12014-06-26
3.12.02014-06-21
3.11.02014-06-19
3.10.52014-06-11
3.10.42014-06-09
3.10.32014-06-05
3.10.22014-06-03
3.10.12014-06-03
3.10.02014-06-02
3.9.02014-05-30
3.8.12014-05-27
3.8.02014-05-21
3.7.02014-05-18
3.6.02014-05-09
3.5.32014-05-08
3.5.22014-04-24
3.5.12014-03-25
3.5.02014-03-06
3.4.82014-01-13
3.4.72013-12-10
3.4.62013-12-01
3.4.52013-11-27
3.4.42013-10-29
3.4.32013-10-23
3.4.22013-10-18
3.4.12013-10-15
3.4.02013-09-07
3.3.82013-09-02
3.3.72013-08-28
3.3.62013-08-27
3.3.42013-07-08
3.3.32013-07-04
3.3.22013-07-03
3.3.12013-06-27
3.3.02013-06-27
3.2.62013-06-02
3.2.52013-05-21
3.2.42013-05-09
3.2.32013-05-07
3.2.22013-05-03
3.2.12013-04-29
3.2.02013-04-15
3.1.22013-04-12
3.1.12013-04-01
3.1.02013-01-25
3.0.62013-01-04
3.0.52012-12-19
3.0.42012-12-05
3.0.32012-11-13
3.0.22012-11-08
3.0.12012-11-01
3.0.02012-10-23
3.0.0rc52012-09-18
3.0.0rc42012-08-30
3.0.0rc32012-08-13
3.0.0rc22012-08-03
3.0.0rc12012-07-24
3.0.0beta72012-07-16
3.0.0beta62012-07-13
3.0.0beta52012-07-03
3.0.0beta42012-06-25
3.0.0beta32012-06-15
3.0.0beta22012-06-06
3.0.0beta12012-06-01
3.0.0alpha52012-05-30
3.0.0alpha42012-05-09
3.0.0alpha32012-05-04
3.0.0alpha22012-04-26
3.0.0alpha12012-04-15
2.5.92012-04-02
2.5.82012-02-08
2.5.72012-02-06
2.5.62012-01-13
2.5.52012-01-08
2.5.42012-01-02
2.5.32011-12-30
2.5.22011-12-10
2.5.12011-11-17
2.5.02011-10-24
2.4.72011-10-05
2.4.62011-08-22
2.4.52011-08-19
2.4.42011-08-05
2.4.32011-07-14
2.4.22011-07-06
2.4.12011-07-06
2.4.02011-06-28
2.3.122011-06-22
2.3.112011-06-04
2.3.102011-05-27
2.3.92011-05-25
2.3.82011-05-24
2.3.72011-05-23
2.3.62011-05-20
2.3.52011-05-20
2.3.42011-05-08
2.3.32011-05-03
2.3.22011-04-27
2.3.12011-04-26
2.3.02011-04-25
2.2.22011-04-12
2.2.12011-04-04
2.2.02011-03-30
2.1.12011-03-29
2.1.02011-03-24
2.0.02011-03-17
2.0.0rc32011-03-17
2.0.0rc22011-03-17
2.0.0rc2011-03-14
2.0.0beta32011-03-09
2.0.0beta22011-03-07
2.0.0beta2011-03-03
1.0.82011-03-01
1.0.72011-02-07
1.0.62011-02-07
1.0.52011-02-05
1.0.42011-02-05
1.0.32011-01-13
1.0.22011-01-10
1.0.12010-12-29
1.0.02010-11-16
1.0.0rc42010-10-14
1.0.0rc32010-09-20
1.0.0rc22010-08-17
1.0.0rc2010-07-28
1.0.0beta22010-07-23
1.0.0beta2010-07-15
0.14.02010-06-15
0.13.02010-06-01
0.12.02010-05-22
0.11.02010-05-06
0.10.12010-05-03
0.10.02010-04-30
0.9.02010-04-14
0.8.02010-03-19
0.7.62010-03-19
0.7.52010-03-16
0.7.42010-03-16
0.7.32010-03-16
0.7.22010-03-16
0.7.12010-03-16
0.7.02010-03-15
0.6.02010-03-11
0.5.02010-03-10
0.4.02010-02-11
0.3.02010-02-11
0.2.12010-02-05
0.2.02010-02-03
0.1.02010-02-03
0.0.22010-01-10
0.0.1Intial Release2010-01-03

Installation

Express JS is the goto framework for developing Web Applications, APIs and almost any kind of Backend using Node.

To install express, all you have to do is run the npm command

npm install express --save

And you’re done.


To create and run a new express server

create a file app.js and add this code

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

// when "/" is opened in url, this function will be called.
app.get('/', function (req, res) {
  res.json({ code: 200, message: 'success' });
})

app.listen( 3000, function () {
  console.log('Express server running at https://localhost:3000');
});
  • In your terminal, run node app.js and
  • Open the url https://localhost:3000 in web browser to see your newly created express server.

It’s also a good idea to install body-parser and express-session along with express as most of the time you will want to read the data sent in POST request and manage user sessions.

Hello World App, using ExpressJS 4 and Node >= 4

Preface

You’ll need node >= 4 and express 4 for this project. You can get the latest node distribution from their download page.

Before this tutorial, you should initialize your node project by running

$ npm init

from the command line and filling in the information you want. Note that you can change the info at any time by editing the package.json file.

Installation

Install express with npm:

$ npm install --save express

After installing Express as a node module, we can create our entry point. This should be in the same directory as our package.json

$ touch app.js

Directory Contents

The folder should have the following directory structure:

<project_root>
 |-> app.js
 |-> node_modules/
 '-> package.json

Code

Open app.js in your preferred editor and follow these four steps to create your first Express app:

// 1. Import the express library.
import express from 'express';

// 2. Create an Express instance.
const app = express();

// 3. Map a route. Let's map it to "/", so we can visit "[server]/".
app.get('/', function(req, res) {
   res.send('Hello World');
});

// 4. Listen on port 8080
app.listen(8080, function() {
   console.log('Server is running on port 8080...');
});

Execution

From the project directory, we can run our server using the command

$ node app.js

You should see the text

$ Our Express App Server is listening on 8080...

Now, visit https://localhost:8080/ and you’ll see the text “Hello World!”

Congratulations, you’ve created your first Express app!

Starting an application with the Express generator

To get started quickly with Express, you can use the Express generator which will create an application skeleton for you.

First, install it globally with npm:

npm install express-generator -g

You may need to put sudo before this command if you get a “permission denied” error.

Once the generator is installed, you can start a new project like this:

express my_app

The above command will create a folder called my_app with a package.json file, an app.js file, and a few subfolders like bin, public, routes, views.

Now navigate to the folder and install the dependencies:

cd first_app
npm install

If you’re on Linux or macOS, you can start the app like this:

DEBUG=myapp:* npm start

Or, if you’re on Windows:

set DEBUG=myapp:* & npm start

Now, load https://localhost:3000/ in your web browser and you should see the words “Welcome to Express”.

Creating an EJS app

a@coolbox:~/workspace$ express --ejs my-app
a@coolbox:~/workspace$ cd my-app
a@coolbox:~/workspace/my-app$ npm install
a@coolbox:~/workspace/my-app$ npm start

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow