I have a question about node js and the express framework.
I want to do the following in my server code:
- Call remote server with xmlhttprequest 1
- Process the response
- Call remote server with xmlhttprequest 2
This is a simplified version of my server code:
app.js:
var express = require('express');
var routes = require('./routes');
var about = require('./routes/about');
var user = require('./routes/user');
var contact = require('./routes/contact')
var http = require('http');
var path = require('path');
var cors = require('cors');
var app = express();
....
app.get('/', routes.index); //<---currently this is handled in index.js, in function index.
index.js:
exports.index = function(req, res){
//xmlhttprequest 1 // <--- send xhr 1
//process data
//xmlhttprequest 2 // <--- send xhr 2
};
I get the message Cant send headers after they are sent
. I read the express documentation and gleaned that this happens when we try to modify the header after the request has been sent. I also read something about middleware, and routing. But the documentation is not clear to me.
Can someone point out what the ideal way to do this would be? I can work with links that have relevant information. I am new to express and node js.
Edit: More details:
The user is on the landing page of my website foo/.
He enters some details, based on which i make the first xmlhttprequest.
- I process the data from API 1, use the processed data as input for API 2, which is again an xmlhttprequest.