我有一个运行在端口3010上的节点服务器,该服务器返回JSON,当我运行它并从浏览器中调用路由时,它会正确响应。
除此之外,我还有另一个在端口3000中运行的节点服务器,应该在第一个中调用路由。
通过console.log,我知道该服务器正在调用: http:// localhost:3010 /
但是在localhost:3010中运行的服务器永远不会收到请求。
它们在Windows 10中的单独cmd shell中运行,浏览器可以同时连接到它们!
有人可以帮助解决这个问题吗?
两台服务器共用:
server.js
const { init } = require('./hapiServer.js');
const start = async () => {
const server = await init();
await server.start();
console.log(`Server running at: ${server.info.uri}`);
Util.Logger.info(`Server running at: ${server.info.uri}`);
};
hapiServer.js
const Hapi = require( '@hapi/hapi' )
const Util = require('@myCompany/configJsonUtility');
const server = Hapi.server( {
port: Util.ConfigJson.server.port,
host: Util.ConfigJson.server.host,
routes: { cors: { origin: ['*'], credentials: true }, timeout: { socket: 60000 * 10 } }
} )
await server.register({
plugin: require('./app/appPlugin')
});
第一台服务器:
config.json
{
"server" : {
"host":"localhost",
"port":"3000",
"pdfEngine":"libreoffice"
},
"dataApi" :{
"ipg":{
"url":"http://localhost:3010/"
}
}
}
appPlugin:
const myPlugin = {
name: 'app',
version: '1.0.0',
register: function (server, options) {
//Add route to server
server.route( {
method: ['POST','GET'],
path: '/',
handler: (request, reply) => {
const res = await requestData(query, queryParameters, headers);
console.log('res:');
console.log(res);
}
} );
}
};
const requestData = (query, queryParameters, headers) => {
return new Promise(((resolve, reject) => {
// this will hold our processed query
let localQuery = query;
// console.log(localQuery);
// Get instSigla to connect to the correct dataApi configuration
const idx = queryParameters.idx.toLowerCase();
// Let's replace parameters in the query with it's value from url
// For now we support just one replace per parameter TODO replace all occurrences of a key
for (const key in queryParameters) {
localQuery = localQuery.replace('##' + key + '##', queryParameters[key]);
}
const requestOptions = {
uri: Util.ConfigJson.dataApi[idx].url + localQuery,
method: 'POST',
headers,
json: true,
rejectUnauthorized: false
};
try {
Request(requestOptions, (err, res, body) => {
if (err) {
return reject(err);
}
Util.Logger.trace( body);
resolve(body);
});
}
catch (err) { // catches errors
Util.Logger.error(err);
reject(err);
}
}));
};
module.exports = myPlugin;
第二个服务器:
config.json:
{
"server" : {
"host":"localhost",
"port":"3010"
}
}
appPlugin:
const TestFile = require('../test.json');
const myPlugin = {
name: 'app',
version: '1.0.0',
register: function (server, options) {
//Add route to server
server.route( {
method: 'GET',
path: '/{any*}',
handler: (request, reply) => {
console.log('request');
console.log(request);
return TestFile;
}
} );
}
};
module.exports = myPlugin;