express get slash value
Code Example:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
- Require the Express module:
Import the Express module using the
require
function and assign it to the variableexpress
.Create an Express application instance:
Create a new instance of the Express application and assign it to the variable
app
by calling theexpress
function.Handle the GET request for the root path ('/'):
- Use the
app.get
method to define a handler for GET requests to the root path ('/') of the server. - The handler function takes two parameters:
req
(the request object) andres
(the response object). In this example, the handler sends the response 'Hello World!' back to the client using the
res.send
method.Start the server:
- Call the
app.listen
method to start the server and make it listen for incoming connections on port 3000. - The second parameter is a callback function that logs a message to the console once the server is running.
This code sets up an Express server that listens for GET requests to the root path ('/') and responds with 'Hello World!'.