【问题标题】:Problem while setting proxy using angular and node使用角度和节点设置代理时出现问题
【发布时间】:2019-01-18 17:19:53
【问题描述】:

我是 Angular 6 的新手。我正在使用 Angular 和节点设置我的项目。在设置时我遇到了一个问题。我得到的错误是:

 Error occurred while trying to proxy request /api from localhost:4200 to http://localhost:5555 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)

我知道对此有很多问题,但我找不到答案。

这里是 proxy.config 文件

{
  "/api/*": {
    "target": "http://localhost:5555",
    "secure": false,
    "changeOrigin": true
  }
}

这是我的节点文件:

var app = require('./app.js');
var debug = require('debug')('mean-app:server');
var http = require('http');
var bodyParser = require('body-parser');
function normalizePort(val) {
   var port = parseInt(val, 10);

  if (isNaN(port)) {
    return val;
  }

  if (port >= 0) {
    return port;
   }

   return false;
}
var port = normalizePort(process.env.PORT || '5555');
app.set('port', port);
 app.get('/api/testapi', function response(req, res) {

  res.send({"test":"test"})
 }) 

var server = http.createServer(app);
server.listen(port);
server.on('listening', onListening);

function onListening() {
  var addr = server.address();
  console.log('Listening on ' + port);
}

【问题讨论】:

  • 您的节点文件中似乎没有以/api/ 开头的端点
  • 您还可以添加如何从您的 Angular 应用程序中调用此 api 吗?
  • 我只是在浏览器中点击网址:localhost:4200/api/testapi

标签: javascript node.js angular angular6


【解决方案1】:

这解决了你的问题。

const PROXY_CONFIG = [
{
    context: [
        "/api"
    ],
    target: "http://localhost:5555",
    secure: false
}
]

 module.exports = PROXY_CONFIG;

【讨论】:

  • 我们在哪里添加这个?
  • 在运行您的应用程序时,将此文件作为 proxy.conf.js 提供。 ng 服务--proxy-config proxy.conf.js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 2021-11-01
  • 2013-12-14
相关资源
最近更新 更多