Node.js ng serve--代理配置proxyconfig.json不工作
我正在使用MEAN stack构建一个应用程序。我使用代理配置文件向后端发出请求,该请求是用NodeJS编写的 proxyconfig.jsonNode.js ng serve--代理配置proxyconfig.json不工作,node.js,angular,proxy,mean-stack,Node.js,Angular,Proxy,Mean Stack,我正在使用MEAN stack构建一个应用程序。我使用代理配置文件向后端发出请求,该请求是用NodeJS编写的 proxyconfig.json { "/api/*": { "target": "https://localhost.com:3333", "secure": false, "changeOrigin": true, "pathRewrite": { "^/api": "https://localhost.com:3333/api"
{
"/api/*": {
"target": "https://localhost.com:3333",
"secure": false,
"changeOrigin": true,
"pathRewrite": {
"^/api": "https://localhost.com:3333/api"
}
}
}
组件文件中的代码
this.http.get("/api/posts",{responseType: 'text'})
.subscribe(
data =>
{
console.log('successs');
},
error =>
{
console.log(error);
}
);
节点JS服务器中的代码
app.get('/api/posts', function(req, res) {
console.log('Posts Api Called');
res.status(200).send({ data: 'somedata' });
});
当我检查来自Chrome的请求时,我得到了500个错误。根本没有调用
GET
方法。原因可能是什么?最后,我犯了一个愚蠢的错误,这对我起了作用
{
"/api": {
"target": "https://localhost:3333/api",
"secure": false,
"changeOrigin": true,
"pathRewrite": {"^/api" : ""}
}
}
最后,我犯了一个愚蠢的错误,这对我起了作用
{
"/api": {
"target": "https://localhost:3333/api",
"secure": false,
"changeOrigin": true,
"pathRewrite": {"^/api" : ""}
}
}
我认为您不需要重写代理配置文件中的路径,尝试不使用
pathRewrite
,也不起作用。您真的在本地服务器上使用https吗?还要了解这是FE上的一个问题,您可以尝试直接从浏览器调用get端点,并检查它是否在all@AmirArbabian我在本地服务器上使用https,当我直接在浏览器中尝试时,端点可以正常工作。我认为您不需要重写代理配置文件中的路径,在没有路径重写的情况下尝试,也不起作用你真的在你的本地服务器上使用https吗?为了理解这是FE上的一个问题,你可以尝试直接从浏览器调用你的get端点,并检查它是否在本地服务器上工作all@AmirArbabian我在本地服务器上使用https,当我直接在浏览器中尝试时,端点工作正常。