Your First Route
Swipe to show menu
A server becomes useful when it can respond to requests. In Express, this is done using routes.
A route defines what should happen when a user accesses a specific URL.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from server');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Here, app.get() defines a route for the '/' path. This path represents the homepage.
When a user opens http://localhost:3000, the server runs the function and sends a response using res.send().
The req object represents the incoming request, and the res object is used to send a response back.
Now your server not only runs but also responds to requests.
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 3
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Section 1. Chapter 3