Chapter 2 of 10
Setting up an Express server takes about 10 lines. Understanding those 10 lines โ what each one does and why โ is the foundation of every Express application.
const express = require("express");
const app = express(); // create the application
const PORT = process.env.PORT || 3000;
// Middleware โ parse JSON request bodies
app.use(express.json());
// A basic route
app.get("/", (req, res) => {
res.json({ message: "NelsonLabs API", version: "1.0" });
});
// Start listening
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
module.exports = app; // useful for testing# Start the server
npm run dev
# In another terminal โ test with curl
curl http://localhost:3000/
# {"message":"NelsonLabs API","version":"1.0"}
# Or use REST Client (VS Code extension)
# Or Postman, Insomnia, Thunder Client