Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: connect ECONNREFUSED and app starts listening #1268

Closed
jcjp opened this issue May 29, 2018 · 5 comments
Closed

Error: connect ECONNREFUSED and app starts listening #1268

jcjp opened this issue May 29, 2018 · 5 comments

Comments

@jcjp
Copy link

jcjp commented May 29, 2018

My app has a base URL of http://localhost:3000, I created a reverse proxy to send requests to http://localhost:3001 with the following configuration and codes:

// app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var httpProxy = require('http-proxy');

var app = express();

const apiProxy = httpProxy.createProxyServer({
  target: 'http://localhost:3001/'
});

app.use('/api', (req, res) => {
  apiProxy.web(req, res);
})

app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'public', 'index.html'));
});

app.use(function (req, res, next) {
  next(createError(404));
});

app.use(function (err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
//apiServer.js
var createError = require('http-errors');
var express = require('express');
var cookieParser = require('cookie-parser');

const session = require('express-session');
const MongoStore = require('connect-mongo')(session);

var app = express();

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

app.get('/books', (req, res) => {
  Books.find((err, books) => {
    if (err) {
      throw err;
    }
    res.json(books);
  })
})

app.listen(3001, (err) => {
  if (err) {
    return console.log(err);
  }
  console.log('API Server is listening on http://localhost:3001');
})

When I try to open my base URL, my web page opens up but when it tries to request on '/books' it does not load and returns an error log, as soon as I try to refresh my app it shows an error on my terminal:

...\node_modules\http-proxy\lib\http-proxy\index.js:120
throw err;
^

Error: connect ECONNREFUSED 127.0.0.1:3001
at Object._errnoException (util.js:1024:11)
at _exceptionWithHostPort (util.js:1046:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1185:14)
API Server is listening on http://localhost:3001

After showing the error, my base URL shows an error and when I go to my proxy URL http://localhost:3001/books I could see the books on my browser.

@denisprsa
Copy link

denisprsa commented May 29, 2018

By looking through the code you defined endpoint target: 'http://localhost:3001/', but in apiServer.js you are listening to http://localhost:3001/books

@jcjp
Copy link
Author

jcjp commented May 29, 2018

@denisprsa should I add the following codes on my apiServer?

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'public', 'index.html'));
});

Whenever I remove the app.get('books') it renders the books list incorrectly.

@jcjp
Copy link
Author

jcjp commented May 31, 2018

It looks like the culprit here is my npm script and nodemon. I have resolved my issue, it turns out that running nodemon with a script of node ./bin/www & apiServer.js only run one server.

@i-am-gopi-i
Copy link

hi @jcjp,
Could you please tell me how did you fix it?
Thanks in advance.

@jcjp
Copy link
Author

jcjp commented Jun 13, 2020

hi @jcjp,
Could you please tell me how did you fix it?
Thanks in advance.

Sorry I was not able to login for a long time, the fix IIRC was to remove & apiServer.js and have it run after node ./bin/www without using nodemon.

@jcjp jcjp closed this as completed Jun 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants