Node / Express.js api run request via terminal

Viewed 1471

I have a node.js / express server where I get information by adding requests on my browser like this:

http://localhost:3000/list/1

This will return me a list of data.

My question is...Is there anyway where I can open a terminal window and make the same request from there?

Can this be done, if so how?

Here's the route code:

app.route('/list/:id')
  .get((req:Request, res: Response) => {
    let id = req.params.id;
    res.status(200).send(list[id]);
});
2 Answers

You can think about using curl or Postman (Postman is not running in a terminal, but worth a look at). With curl you would do curl http://localhost:3000/list/1 for a simple GET-request.

Let's assume you have the following index.js running with node ./index.js:

var express = require('express');
var app = express();

app.get('/foo', function (req, res) {
  res.status(200).json({ name: 'foo' });
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Then, curl localhost:3000/foo prints {"name":"foo"}.

Related