Angular 2 localhost and Backendserver Connection

拥有回忆 提交于 2019-12-01 11:34:38

You can set up proxy in your local environment.

In your package.json add in the script "serve-dev": "<startApplication> --sourcemap=false --proxy-config proxy.config.json".

And run it using npm run serve-dev.

And proxy.config.json file should look like this:

{
  "/api/*":{
    "target":"http://localhost:5005",
    "secure": false,
    "logLevel": "debug"
  }
}

And when you call endpoint in your service just get should be like: this._http.get('./api/myEndpoint').

Set this up before you API route and after your app use

app.use(express.static(publicPath)); // Set the path for express to use

// Add headers
app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization');
  next();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!