0

I need to call a third party API server in NodeJS, as you can see below, I need to use req and resp inside request() method.

Since there are a lot of APIs that I need to use, I don't want to set headers and set response cookies every time I use request().

Is there anything I can do to intercept request()?

router.post('/register', function (req, resp) {
    var api = "/user/register"
    var data = req.body)
    request({
        url: api,
        method: "POST",
        json: true,
        headers: {'session_id' : req.cookies.session_id},
        body: {
            "tel": data.tel, "code": data.code,
            "password": data.passwd, "referee": data.recommend
        }
    }, function (error, response, body) {
        resp.cookies('session_id', response.headers['sessionId'])
        resp.json(body)
    })
})
1
  • request package doesn't know nothing about express and him request and response objects, so you can't intercept. But you can create two simple functions which will extract required info from req and update res. Just call these functions in every call of request. Commented Apr 9, 2016 at 14:02

2 Answers 2

1

If I understood correctly, you want to avoid passing the same options to request everytime?

You can simply create a helper function that will call it for you:

function apiRequest(url, req, cb) {
  request({
  url: url,
  method: "POST",
    json: true,
    headers: {'session_id' : req.cookies.session_id},
    body: {
        "tel": req.body.tel, "code": req.body.code,
        "password": req.body.passwd, "referee": req.body.recommend
    }
  }, cb)
}

// Your previous code becomes
router.post('/register', function (req, resp) {
  apiRequest("/user/register", req, function (err, response, body) {
    resp.cookies('session_id', response.headers['sessionId'])
    resp.json(body)
  })
});

Of course, it would be better if you added error handling to this (e.g. checking that req isn't undefined, that req.body.X exists, etc.)

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

Comments

0

Not very elegant, but you could always create your own wrapper around request

e.g.

myrequest.js

var request = require('request');

exports.postJsonWithCookie = function(url, payload, req, resp) {
  request({
    url: url,
    method: "POST",
    json: true,
    headers: {'session_id' : req.cookies.session_id},
    body: payload
  }, function (error, response, body) {
    resp.cookies('session_id', response.headers['sessionId'])
    resp.json(body)
  });
}

then in your route definition you could do

var request = require('./myrequest');

router.post('/register', function (req, resp) {
  var payload = {
    "tel": req.body.tel, 
    "code": req.body.code,
    "password": req.body.passwd, 
    "referee": req.body.recommend
  }
  request.postJsonWithCookie("/user/register", payload, req, resp);
});

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.