---
title: "Installing Express"
date: "2015-03-17"
category: "node"
slug: "installing-express"
---

[Express](http://expressjs.com/) is a minimalist Node.js web framework inspired by Sinatra for Ruby. It is part of the [MEAN](http://mean.io) stack: **M**ongoDB, **E**xpress, **A**ngular.js, and **N**ode.js. MongoDB can be configured separately via [[running-mongodb]].

Express is supported on all [[determining-platform-version]] platforms using Passenger to manage isolated processes.

## Quickstart

All steps are performed from the [[accessing-terminal]]:

1. **Prerequisite:** Create a Passenger-compatible [[passenger-application-layout]].
    - In this example, the app resides in `/var/www/express`:

        express
        ├── app.js
        ├── public
        │   └── .htaccess
        └── tmp

2. From the root directory (`/var/www/express`), install Express locally with [[running-node-js]]:
    - npm install express

3. Create a startup file named `app.js` in the `express/` directory with the following sample application:

    var 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)
    })

4. Add a Passenger directive to inform the web server this is a Node.js application:

    echo "PassengerNodejs /usr/bin/node" > public/.htaccess

5. Connect `public/` to a [[creating-subdomain]] within the [[logging-into-the-control-panel]]

### Using Express Generator

Express Generator scaffolds a new application filesystem. Install it globally via npm:

npm install -g express-generator

Then run `express <appname>` to create a new app, e.g. `cd /var/www && express express` creates an app in `/var/www/express`. The generated layout is [[passenger-application-layout]] with Passenger.

Change to the new directory and run `npm install` to install dependencies.

**Note:** The `-g` flag installs the package globally under `/usr/local`. Use it for loosely-coupled tools whose binaries should be accessible system-wide via `/usr/local/bin`.

**Important:** When using Express Generator, the startup file is `bin/www` (not `app.js`). To make this work with Passenger, add `PassengerStartupFile www/bin` to `.htaccess` in `public/`.

## See Also

- Express [API documentation](http://expressjs.com/api.html)
- Express guide: [Routing](http://expressjs.com/guide/routing.html)
