Requests timing out running Node with pm2 on ubuntu

49 views Asked by At

I have been learning node/express and I've built an api on my local machine that points to a mongo atlas database and it works great.

So I deployed it on a Digital ocean vpc running Ubuntu 23.10 with node v18.13.0. When I run my node server using npm start, everything works great - I can see the outcome of my requests in my mongo database.

So I installed pm2 and all my requests that talk to my database seem to time out, this is what I see in pm2 logs:

0|server  | MongooseError: Operation quotes.find() buffering timed out after 10000ms
0|server  |     at Timeout.<anonymous> (/var/www/api.adfinlaysonguitars.co.uk/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:186:23)
0|server  |     at listOnTimeout (node:internal/timers:564:17)
0|server  |     at process.processTimers (node:internal/timers:507:7)
0|server  | GET /quotes 500 10011.722 ms - 12

My first thought was that it might be because my mongo db is in ireland and my linux server is in USA, but the stack worked perfectly well before attempting to start node with pm2

I appreciate any help understanding where I am going wrong, let me know if I can provide any more information.

The command I am running to start is pm2 start server.js but I also tried pm2 start app.js which gives a bad gateway error

This is my server.js file:

const http = require('http');
const app = require('./app');
const port = process.env.port || 3000;
const server = http.createServer(app);

server.listen(port);

and this is my app.js file:

const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const contactRoutes = require('./api/routes/contacts');
const userRoutes = require('./api/routes/users');
const configuratorRoutes = require('./api/routes/configurators');
const quoteRoutes = require('./api/routes/quotes');

mongoose.connect("mongodb+srv://"+process.env.MONGO_ATLAS_USER+":"+process.env.MONGO_ATLAS_PW+"@******.mongodb.net/?retryWrites=true&w=majority");

app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header(
        'Access-Control-Allow-Headers',
        'Origin, X-Requested-With, Content-Type, Accept, Authorization'
    );

    if (req.method == 'OPTIONS') {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, PUT');
        return res.status(200).json({});
    }
    next();
});

// routes to handle requests
app.use('/contacts', contactRoutes);
app.use('/user', userRoutes);
app.use('/configurators', configuratorRoutes);
app.use('/quotes', quoteRoutes);

// Any other routes get 404 response
app.use((req, res, next) => {    const error = new Error('Not found');
    error.status = 404;
    next(error);
});

app.use((error, req, res, next) => {
    res.status(error.status || 500);
    res.json({
        error: {
            message: error.message
        }
    });
});

module.exports = app;

I tried also starting pm2 in this way: pm2 start server.js --wait-ready --listen-timeout 1000 which made no difference.

1

There are 1 answers

0
Ashley Finlayson On

This isn't an answer to the issue however I have worked around this by using Supervisor instead of pm2