1

I have been using jQuery's $.ajax() functionality to make asynchronous http requests from my Node server to other external API servers.

I now want to upgrade my libraries but I'm getting

....
/jquery/node_modules/jsdom/lib/jsdom/level1/core.js:418
   set nodeName() { throw new core.DOMException();},
               ^^

SyntaxError: Setter must have exactly one formal parameter.
  1. How can I fix this error?
  2. Is there any library that is equally flexible as $.ajax() is? I'm particularly interested in the promise and $.ajaxPrefilter() functionalities.

1 Answer 1

2

Yes, node has a module called request that can do a lot more than you're probably used to with $.ajax. Basic example from their page:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage.
  }
})

https://github.com/request/request

As far as promises go, you can promisify anything you want with a library like bluebird: http://bluebirdjs.com/docs/getting-started.html

jQuery on the server is wholly unnecessary -- just tear the bandaid off quickly and it'll hurt less :)

Edit

Adding defaults is very easy. Just do

var req = request.defaults({
  token: myToken,
  ...
})

var payload = { ... }

req.get(payload, function(err, res, body){ 
  ... 
})
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks tyler. I checked request but I couldn't find a way to add a prefilter to it. Do you have any ideas on that? I basically want to add a token to every request made.
They call them defaults, not prefilter. I've added an example above.
should it be request.defaults({qs: {token: myToken}}) ?
Depends on your auth scheme. I use request.defaults({ auth: { bearer: myToken } }), but you can do it however you want.
request is deprecated and I don't find a clear alternative...

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.