Skip to content

Instantly share code, notes, and snippets.

@kevinohara80
Created September 7, 2012 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinohara80/3669419 to your computer and use it in GitHub Desktop.
Save kevinohara80/3669419 to your computer and use it in GitHub Desktop.
Simple nested callback example
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