0

I have a Node.js app running with Expressjs. I have the typical setup right now, using view jade files and route js files to process GET and POST requests. An example is as follows:

router.post('/postaction', function(req, res) {

    // elements from the actual jade view itself
    var one = req.body.from;
    var two = req.body.to;

    // do something

});

router.get('/getaction', function(req, res) {

    // elements from the actual jade view itself
    var one = req.body.from;
    var two = req.body.to;

    // do something

});

This works, I can carry out GET and POST requests. For example, a button might invoke the POST action. My question is, how can I use a separate script or npm module to invoke the same GET and POST requests without having to do it via the views? Ideally, I would have something like this:

somescript-get <param1>
somescript-post <param1> <param2>

with the params being akin to req.body.from and req.body.to. Essentially, I am looking to emulate the info from the page without having to use the page itself.

I am new to Node.js and would really appreciate some help. Please point out if I haven't explained something enough and I'll try to.

Thank you

1 Answer 1

2

An often-used tool for that is curl.

For instance, to make a GET request:

$ curl 'http://your.url/getaction?from=FOO&to=BAR'

To make a POST request:

$ curl -XPOST http://your.url/postaction -d 'from=FOO&to=BAR'

An alternative, one that I prefer myself, is httpie.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply. I resorted to using the npm request module, which is looking promising.
@intl sorry, perhaps I misunderstood :) I thought you were looking for a standalone script, but if you were looking for a module to perform those requests with, request certainly is a good one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.