ajax - How do I receive an http request with a Node.js server and then pass that request along to another server? -
there's lot going on here i'll simplify pseudo example. forget security , whatnot minute here. point understand functionality.
let's i'm running local web server node.js dev website. in website, user should able create new account. account information submitted via ajax node server. need node server take incoming request , pass along server gives me access database. couchdb, example.
so here's pseudo example of i'd happen.
in client's browser:
$.ajax({ url: './database_stuff/whatever', // points node web server method: 'post', data: {name: 'billy', age: 24} }); in node web server:
var http = require('http'), dbserver = 'http://127.0.0.1:5984/database_url'; http.createserver(function (req, res) { /* figure out need access database then... */ // magically pass request on db server http.magicpassalongmethod(req, dbserver, function (dbresponse) { // pass db server's response client dbresponse.on('data', function (chunk) { res.end(chunk); }); }) }).listen(8888); make sense? what's best way pass original request along server , pass response client?
if server @ dbserver url supports streaming like
var request = require('request'); req.pipe(request.post(dbserver)).pipe(res) where request module, more info here https://github.com/mikeal/request
this quite readable , easy implement, if whatever reason cannot take need request , manually post it, take response , res.send client.
sorry if there's error in code, haven't tested point should clear, if it's not ask away.
Comments
Post a Comment