我的节点应用程序的路由文件夹中有两个文件,例如fetchCity.js和addNewDevice.js。我想将请求参数从addNewDevice.js转发到fetchCity.js并在addNewDevice.js文件中处理响应。我试过下面的代码,但没有工作。NodeJS中的请求转发
var express = require('express');
module.exports = function(app){
var cors = require('cors');
var coptions = {
"origin": "*",
"methods": "GET,HEAD,PUT,POST,OPTIONS",
"preflightContinue": false,
"allowedHeaders":['Content-Type']
}
var db = require('./dbclient');
var bodyParser = require('body-parser');
app.use(cors(coptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.post('/newBinDevice', function(req, res, next) {
var did = req.body.deviceid;
var sver = req.body.swver;
var city = req.body.city;
var circle = req.body.circle;
app.post('/fetchCityArea',function(req,res){
console.log('Response from fetchCityArea is ' + JSON.stringify(res));
});
});
}
我认为它会重定向用户和响应将被发送给用户。用户可以随时向/ fetchCityArea发送请求。但是,当他们向/ addNewDevice发送请求时,我需要在/ addNewDevice中处理/ fetchCityArea的响应。如果我错了,请纠正我。因为在服务器代码中使用重定向方法后,我没有找到如何获取响应对象。 –
是的,你是对的。你是否也在app.post处理程序中处理请求? –
是的,我想从/ addNewDevice发送请求到/ fetchCityArea,并且在收到响应后,我想在/ addNewDevice中进一步处理它。预期的行为与Java中的RequestDispatcher.forward方法类似。 –