After creating your package.json
file and installing your dependencies, you can create your first Express application by adding your already familiar server.js
file with the following lines of code:
const express = require('express'); const app = express(); app.use('/', (req, res) => { res.status(200).send('Hello World'); }); app.listen(3000); console.log('Server running at http://localhost:3000/'); module.exports = app;
You should have already recognized most of the code. The first two lines require the Express module and create a new Express application object. Then, we use the app.use()
method to mount a middleware function with a specific path and the app.listen()
method to tell the Express application to listen to port 3000
. Note how the module.exports
object is used to return the app
object. This will later help you load and test your Express application.
This new code should also be familiar to you because it resembles the code you used...