Step 2: Hello Express
Here is a minimal Express application:
// index.js file
const express = require("express");
const app = express();
const port = 5000;
app.get("/", (req, res) => {
res.send("Hello Express!");
});
app.listen(port, () => {
console.log(`Express app listening at http://localhost:${port}`);
});
Go to terminal and run node index.js
. You will see the following message printed in the terminal:
Express app listening at http://localhost:5000
While the Express app is running, open your browser and go to http://localhost:5000/. You will see the "Hello Express" message!
Recall: to stop the app, you must halt the process by pressing Ctrl
+ C
.
The application we've built here with Express is identical to the one we had built earlier with Node's HTTP Module. In fact, Express is built on top of Node's HTTP module but it adds a ton of additional functionality to it.
Express provides a clean interface to build HTTP server applications. It provides many utilities, takes care of nuances under the hood, and allows for many other packages to be added as middleware to further the functionality of Express and your server application.