To manage routes in a simple Express.js server, follow these easy steps:
Set Up the Server: First, create your server using Express.
const express = require('express'); // This makes Express available
const app = express(); // This sets up the app
const PORT = 3000; // You choose the port number
Define Your Routes: Use app.get()
, app.post()
, and so on to set up different paths.
app.get('/', (req, res) => {
res.send('Welcome to the Home Page!'); // This shows a message when the Home Page is opened
});
Start the Server: Finally, tell your server to listen on the port you've chosen.
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`); // This confirms the server is running
});
By following these steps, you can easily control different routes (endpoints) and respond properly!
To manage routes in a simple Express.js server, follow these easy steps:
Set Up the Server: First, create your server using Express.
const express = require('express'); // This makes Express available
const app = express(); // This sets up the app
const PORT = 3000; // You choose the port number
Define Your Routes: Use app.get()
, app.post()
, and so on to set up different paths.
app.get('/', (req, res) => {
res.send('Welcome to the Home Page!'); // This shows a message when the Home Page is opened
});
Start the Server: Finally, tell your server to listen on the port you've chosen.
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`); // This confirms the server is running
});
By following these steps, you can easily control different routes (endpoints) and respond properly!