const express = require('express') const app = express() app.get('/', (req, res) => res.send('Hello World!')) app.listen(3000, () => console.log('Example app listening on port 3000!'))
This 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.
RunKit is a third-party service not affiliated with the Express project.
First create a directory named
myapp, change to it and run
npm init. Then install
express as a dependency, as per the installation guide.
myapp directory, create a file named
app.js and copy in the code from the example above.
req (request) and
res (response) are the exact same objects that Node provides, so you can invoke
req.on('data', callback), and anything else you would do without Express involved.
Run the app with the following command:
$ node app.js
http://localhost:3000/ in a browser to see the output.