1

i have a API called /validate which returns true or false value . I want true/false reponse to use in other API in nodejs .Please help

app.get('/validate',new TestController()) above api will give response like true or false

now in below api i want to call /validate and according to reponse i want to send result to user

app.get('/check',new Validator())

i used require('request') but getting error not found

6
  • You will need to add some more detail to your question on what you are doing and what you have tried so far. Is this a HTTP API you are calling out to? Do you want to use the response from that API in your application somewhere? Can you post a sample of where you want to use this value? Commented Mar 26, 2016 at 9:25
  • If you need to call URLs use request module github.com/request/request Commented Mar 26, 2016 at 9:27
  • Usually, the better answer here is to take the code in your /validate route and beak it out into a function that can be called separately from a route so you pass it some input and it returns some output from the function. Then, you can call that function from both routes and reuse the same code in two places without having to actually make another TCP request. This is called "factoring common code" so it can be used in multiple places. Commented Mar 26, 2016 at 10:07
  • @jfriend00 I guess he already knows that, and wants to communicate between 2 different servers, doing API requests (GET requests in this case) Commented Mar 26, 2016 at 10:29
  • 2
    @JoeTannoury - He doesn't say two separate servers anywhere in the question. Perhaps the OP could clarify. I thought he was trying to call his API on the same server. Commented Mar 26, 2016 at 10:37

2 Answers 2

1

I know this question is more than one year old, but:

The easiest way I found to consume data from your own self-hosted API is:

router.get('/', function(req, res, next) {
    /* Find own IP and Port */
    require('dns').lookup(require('os').hostname(), function (error, address) {
        /* Assemble API URL (yours might be a little different) */
        ownAPI = "http://" + address + ":" + req.app.get('port') + "/api";

        /* Request from your own API */   
        request(ownAPI, function(error, response, body) {
            // Whatever you wanna do here
Sign up to request clarification or add additional context in comments.

Comments

0

If you have a successful api server, for example if you go into your browser and go to url/validate and it gives you a response (true or false or whatever in your case) then your request using the module "request" is incorrect.

In this case, your api endpoint uses GET to request. ~(someone edit me, I'm not good at english =P)

request.get('/validate', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body); // body is either 'true' or 'false'
    }
});

Comments

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.