Friday, May 28, 2021

request parameters

 You can fetch parameters from request of client browser using the URL parameters.

http://localhost:3000/api/courses/1

http://localhost:3000/api/courses/1/456

/? query parameters are optional and can be fetched from req.query

const express = require("express");

const app = express();

app.get("/", (reqres=> {
  res.send("Hello");
});

app.get("/api/courses", (reqres=> {
  res.send([123]); //courses array
});

app.get("/api/courses/:id", (reqres=> {
  res.send(req.params.id); //courses array
});

app.get("/api/courses/:id/:value", (reqres=> {
  res.send(req.params); //courses array
});

//app.listen(3000, () => console.log("listening"));
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}`));

No comments:

Post a Comment