-1

I want to get URL of route in express js.

http://localhost:9310/api/arsys/v1/entry/SimpleForm5/?fields=Request ID,Assigned To,Status,Submitter,Create Date&q=Status=New&offset=5&sort=Create Date.desc

I want only http://localhost:9310/api/arsys/v1/entry/SimpleForm5/

I tried const url = req.protocol + '://' + req.headers.host + req.url; which gives http://localhost:9310/SimpleForm5/?fields=Request%20ID,Assigned%20To,Status,Submitter,Create%20Date&q=Status=New&offset=5&sort=Create%20Date.desc

But it does not give /api/arsys/v1/entry/. Also I does not want query params in output.

Please help

raju
  • 6,448
  • 24
  • 80
  • 163

1 Answers1

1

To retrieve the url without queryParams however with host, originalUrl, You can go that way:

const urlWithoutQueryParams = req.protocol + '://' + req.headers.host + url.parse(req.originalUrl).pathname;

For example, consider that code:

router.route('/arsys/v1/entry/SimpleForm5/')
  .get(async (req, res) => {
    try {
      console.log(req.protocol + '://' + req.headers.host + url.parse(req.originalUrl).pathname)
      return res.status(200).send({ message: "OK" });
    } catch (error) {
      return res.status(500).send({ message: "Failure" });
    }
  });

app.use('/api', router);

app.listen(8080, () => {
  log.info('app started')
})

And when You send GET to:

http://localhost:8080/api/arsys/v1/entry/SimpleForm5/?fields=Request ID,Assigned To,Status,Submitter,Create Date&q=Status=New&offset=5&sort=Create Date.desc

Result is:

http://localhost:8080/api/arsys/v1/entry/SimpleForm5/
KarlR
  • 1,545
  • 12
  • 28