简体   繁体   中英

Error occured while trying to proxy to: localhost:4200/api/v1/generate_uid

I am following a tutorial ( https://levelup.gitconnected.com/simple-application-with-angular-6-node.js-express-2873304fff0f ) on creating an app with Angula CLI, Node.js and Express. I use a proxy to start the app, the file defining the proxy looks like this:

{
  "/api/*": {
    "target": "http://localhost:3000",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  }
}

The command I use to start the app is this one:

ng serve --proxy-config proxy.conf.json

The tutorial said that:

All requests made to /api/... from within our application will be forwarded to http://localhost:3000/api/...

To be honest, I don't really know how it is supposed to work, because when I launch the app, I still use the URL: http://localhost:4200/.

But I didn't have a problem until now. I just created a route with Express.js at the Endpoint /api/v1/generate_uid.

But the problem is when I go to http://localhost:4200/api/v1/generate_uid it shows this message:

Error occured while trying to proxy to: localhost:4200/api/v1/generate_uid .

The following is the message I get in the console:

[HPM] Error occurred while trying to proxy request /api/v1/generate_uid from localhost:4200 to http://localhost:3000 (ECONNREFUSED) ( https://nodejs.org/api/errors.html#errors_common_system_errors )

And when I go to http://localhost:3000 it always says that the connection has failed.

For further references, Here are the app.js of my express API and generate_uid.js which defines the route:

app.js

var express = require('express');
var uid = require('uid-safe');

var router = express.Router();

router.get('/', function(req, res, next) {
    var strUid = uid.sync(18);
    res.json({guid: strUid});
});

module.exports = router;

generate_uid.js

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var users = require('./routes/users');
var generate_uid = require('./routes/generate_uid');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser())

app.use('/api/v1/users', users);
app.use('/api/v1/generate_uid', generate_uid);

module.exports = app;

So I really don't know what the solution is. Thanks in advance for your answers !!

As said in the comments, it looks like the app doesn't have the .listen() function, which is very important to bind to the port.

app.listen(3000, () => {
  console.log("Server started in port 3000!");
});
    {
   "/api": {
      "target": "http://localhost:3000",
      "secure": false,
      "changeOrigin": true,
      "logLevel": "info"
   },
   "pathRewrite": {
      "^/api": ""
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM