- Use the
express
module to create an instance of the Express application:
const express = require('express');
const app = express();
- Define a route using the
get
method of the Express application instance:
app.get('/example', function(req, res) {
// Route handling logic goes here
});
- Access parameters and query parameters from the request object (
req
):
app.get('/example/:param', function(req, res) {
const paramValue = req.params.param; // Access parameter
const queryParamValue = req.query.q; // Access query parameter
});
- Set response status and send a response:
app.get('/example', function(req, res) {
res.status(200).send('Response sent');
});
- Start the Express application to listen for incoming requests on a specified port:
const PORT = 3000;
app.listen(PORT, function() {
console.log(`Server is running on port ${PORT}`);
});