Skip to main content
The express module exports the Express factory function and various utility functions and constructors.

express()

Creates an Express application. The express() function is the top-level function exported by the express module.
const express = require('express');
const app = express();

Example

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

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000);

Exported Properties

The express module exports several additional properties for creating routers, middleware, and accessing prototypes.

express.application

The Application prototype, which is used to extend the app object.
const express = require('express');
const app = express.application;

express.request

The Request prototype, used to extend req objects.
const express = require('express');
const req = express.request;

express.response

The Response prototype, used to extend res objects.
const express = require('express');
const res = express.response;

Exported Constructors

express.Router([options])

Creates a new router object. A router behaves like middleware and can be used to organize routes.

Example

const express = require('express');
const router = express.Router();

router.get('/users', (req, res) => {
  res.send('Users list');
});

app.use('/api', router);

express.Route

Exposed Route constructor from the router module. Used internally by Express.

Exported Middleware

Express exports several built-in middleware functions from external packages.

express.json([options])

Middleware that parses incoming requests with JSON payloads. Based on body-parser.

Example

app.use(express.json());

app.post('/api/users', (req, res) => {
  console.log(req.body); // Access parsed JSON
  res.json({ received: true });
});

express.urlencoded([options])

Middleware that parses incoming requests with URL-encoded payloads. Based on body-parser.

Example

app.use(express.urlencoded({ extended: true }));

app.post('/login', (req, res) => {
  const { username, password } = req.body;
  res.send(`Login attempt for ${username}`);
});

express.raw([options])

Middleware that parses incoming request payloads into a Buffer. Based on body-parser.

Example

app.use(express.raw({ type: 'application/octet-stream' }));

app.post('/upload', (req, res) => {
  console.log(req.body); // Buffer
  res.send('Received');
});

express.text([options])

Middleware that parses incoming request payloads into a string. Based on body-parser.

Example

app.use(express.text());

app.post('/notes', (req, res) => {
  console.log(req.body); // String
  res.send('Note saved');
});

express.static(root, [options])

Serves static files from the specified directory. Based on serve-static.

Example

app.use(express.static('public'));
app.use('/static', express.static('public'));

// Serve with options
app.use(express.static('public', {
  maxAge: '1d',
  dotfiles: 'ignore',
  index: 'index.html'
}));

Complete Example

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

// Use built-in middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));

// Create a router
const router = express.Router();
router.get('/users', (req, res) => {
  res.json([{ id: 1, name: 'John' }]);
});

app.use('/api', router);

app.listen(3000, () => {
  console.log('Server running on port 3000');
});