在express
配置中已经使用了中间件http-proxy-middleware
并且配置了转发
app.use('/api', proxy({
target: 'http://x.x.x.x.',
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}));
而在 app.get 方法中使用 axios 的时候会报错:
app.get('/index/test', function(req, res) {
axios.get('/api/index/getdata.html?page=1') // 这里如果写成 http://x.x.x.x/index/getdata.html?page=1 就不会报错
.then(result=>{
console.log(result);
return res.send('test');
})
});
output
(node:12952) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Request failed with status code 404
(node:12952) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
其中我想要使用 /api ,不知道怎么实现?
你这里需要指定协议,域名和端口呀,不然它怎么知道你去请求哪儿;
不像浏览器,浏览器访问一个网站,这些都知道了的
在这儿可以使用
http://127.0.0.1:port/api