我刚在代码中遇到问题。尽管经过我的努力,我仍然找不到解决方案。有人可以帮助我。非常感谢你。 我试图在Google上找到解决方案,但没有任何解决方案。我希望知道发生了什么以及如何解决此问题
const express = require('express');
const app = express();
const request = require('request');
var serverInfo = {
application: 'Test',
startTime: new Date(),
db: {},
lastTime: 0,
last2Time: 0
};
app.use(function(req, res, next) {
serverInfo.lastTime = new Date();
if (req.url.indexOf('//INFO//') === 0)
return res.status(200).json(serverInfo);
request({
baseUrl: 'htttp://api.localhost/',
uri: req.url,
method: req.method,
headers: {},
body: req.body,
followRedirect: false,
encoding: 'utf8',
gzip: true,
agent: ''
}, function(error, response, body) {
if (error) return console.error(error);
if (response.statusCode === 404)
return res.status(404).send('Hello World !');
req.res = response;
try {
req.res.body = JSON.parse(body);
}
catch (e) {
console.error(e);
req.res.body = body;
}
next();
return serverInfo.last2Time = new Date();
});
});
app.get('/admin', function(req, res, next) {
res.send('Admin');
});
app.get('*', function(req, res) {
return res.status(req.res.statusCode).json(req.res.body); //Error here
});
app.listen(process.env.PORT, function() {
return console.log('Port: ' + process.env.PORT);
});
和我的错误
TypeError: res.get is not a function
at IncomingMessage.fresh (/home/ubuntu/workspace/proxy/node_modules/express/lib/request.js:474:19)
at ServerResponse.send (/home/ubuntu/workspace/proxy/node_modules/express/lib/response.js:206:11)
at ServerResponse.json (/home/ubuntu/workspace/proxy/node_modules/express/lib/response.js:267:15)
at /home/ubuntu/workspace/proxy/index.js:49:43
at Layer.handle [as handle_request] (/home/ubuntu/workspace/proxy/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/workspace/proxy/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/ubuntu/workspace/proxy/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/workspace/proxy/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/workspace/proxy/node_modules/express/lib/router/index.js:281:22
at param (/home/ubuntu/workspace/proxy/node_modules/express/lib/router/index.js:354:14)
感谢所有查看此文章的人!