Overview
Express is a Node.js framework inspired by Sinatra for Ruby: it’s based on minimalism with a penchant for performance. Express is part of the MEAN fullstack: MongoDB, Express, Angular.js, and Node.js. MongoDB may be setup in a separate guide.
Express is supported on all v6+ platforms using Passenger to manage isolated processes.
Quickstart
All steps are done from the terminal:
- PREREQUISITE: create a Passenger-compatible filesystem layout
- In this example, our app will reside in
/var/www/express
. The filesystem layout looks like:express ├── app.js ├── public │ └── .htaccess └── tmp
- In this example, our app will reside in
- From the root directory,
/var/www/express
, install Express locally with npm:-
npm install express
-
- Now create a startup file named
app.js
withinexpress/
. Copy and paste the following as a sample application in the root folder: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) })
- Inform Passenger that the app should be launched as Node.js application
echo "PassengerNodejs /usr/bin/node" > public/.htaccess
- Lastly, connect
public/
to a subdomain within the control panel - Enjoy!
Using Express Generator
Express Generator is a separate application to facilitate filesystem creation for an app. It may be installed separately from npm:
npm install -g express-generator
Now run express <appname>
where appname is a new app to create, e.g. cd /var/www && express express
to create a new app located in /var/www/express
. The application, express
, will scaffold a new filesystem layout that is compatible with Passenger.
Change directories to the newly-created app root, and run npm install
to install dependencies.
Note: astute readers will note that npm
is invoked first without -g
, then with -g
. -g is a flag that installs the package globally in /usr/local
. In certain situations, where an application is loosely-coupled and serves no integral function, placing it under /usr/local
would be better so that binaries are picked up under /usr/local/bin
.
Important: once generated the startup file is located as bin/www
. app.js
is a separate application launched after initialization. To make this work with Passenger, add PassengerStartupFile www/bin
to .htaccess
in public/
.
See also
- Express demo on Sol, a v6 platform
- Express API documentation
- Express guide: Routing (start here and continue onward)