Created
September 7, 2012 20:38
-
-
Save kevinohara80/3669419 to your computer and use it in GitHub Desktop.
Simple nested callback example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var request = require('request'); | |
var app = module.exports = express(); | |
app.configure(function() { | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
}); | |
app.post('/in', function(req, res) { | |
// we are going to take the body from the post and | |
// forward it to the endpoint | |
request.post({url: 'http://some.endpoint.com', body: req.body}, function(err, res, body) { | |
// now we will respond to the client in the callback | |
// from the request to the external endpoint. | |
if(err) { | |
// just forward the error back to the client when there is an error | |
// from the endpoint | |
res.send(err); | |
} else { | |
// send the body that we get in the callback on success | |
res.send(body); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment