This is essentially going to be the simplest Express app you can create. It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories for various purposes.
First create a directory named myapp
, change to it and run npm init
. Then install express
as a dependency, as per the installation guide.
In the myapp
directory, create a file named app.js
and add the following code:
var express = require('express') var app = express() app.get('/', function (req, res) { res.send('Hello World!') }) app.listen(3000, function () { console.log('Example app listening on port 3000!') })
The app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/
) or route. For every other path, it will respond with a 404 Not Found.
The req
(request) and res
(response) are the exact same objects that Node provides, so you can invoke req.pipe()
, req.on('data', callback)
, and anything else you would do without Express involved.
Run the app with the following command:
$ node app.js
Then, load http://localhost:3000/
in a browser to see the output.
© 2016 StrongLoop, IBM, and other expressjs.com contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v3.0.
http://expressjs.com/en/starter/hello-world.html