I built a NodeJS Express & MongoDB web application that is working well locally.
I tried to deploy it.
Eventhough, the application is partially working in production mode, some of its pages are not displaying.
For example, I'm not able to access the blog page and the following message error displays instead of the content of the page:
Incomplete response received from application
Therefore, I logged the blog page and got the error message below:
App 814821 output: node:internal/process/promises:279 App 814821 output: triggerUncaughtException(err, true /* fromPromise /); App 814821 output: ^ App 814821 output: [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#".] { App 814821 output: code: 'ERR_UNHANDLED_REJECTION' App 814821 output: } [ W 2022-11-19 10:30:01.7998 813657/Tg age/Cor/Con/InternalUtils.cpp:96 ]: [Client 5-1] Sending 502 response: application did not send a complete response [ N 2022-11-19 10:30:01.8095 813657/Ti age/Cor/CoreMain.cpp:1147 ]: Checking whether to disconnect long-running connections for process 814821, application /home/raso1970/node-com4muz (development) App 815207 output: node:internal/process/promises:279 App 815207 output: triggerUncaughtException(err, true / fromPromise */); App 815207 output: ^ App 815207 output: [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#".] { App 815207 output: code: 'ERR_UNHANDLED_REJECTION' App 815207 output: } [ W 2022-11-19 10:30:03.7100 813657/Ti age/Cor/Con/InternalUtils.cpp:96 ]: [Client 6-1] Sending 502 response: application did not send a complete response [ W 2022-11-19 10:30:05.0596 813657/T3 age/Cor/App/Poo/AnalyticsCollection.cpp:102 ]: Process (pid=815207, group=/home/raso1970/node-com4muz (development)) no longer exists! Detaching it from the pool. [ N 2022-11-19 10:30:05.0597 813657/T3 age/Cor/CoreMain.cpp:1147 ]: Checking whether to disconnect long-running connections for process 815207, application /home/raso1970/node-com4muz (development)
I guess the issue comes from MongoDB? Maybe I did not set up the connection correctly?
Here is my code related to the MongoDB database connection:
data\database.js:
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
let database;
let mongodbUrl = 'mongodb://127.0.0.1:27017';
let MONGODB_URL = 'mongodb+srv://<username>:<password>@cluster0.42o6qd6.mongodb.net/?retryWrites=true&w=majority'
if (process.env.MONGODB_URL) {
mongodbUrl = process.env.MONGODB_URL;
}
async function connect() {
const client = await MongoClient.connect(mongodbUrl);
database = client.db('com4muz-blog');
}
function getDb() {
if (!database) {
throw { message: 'Database connection not established!' };
}
return database;
}
module.exports = {
connectToDatabase: connect,
getDb: getDb
};
Indeed, I did write my username and password instead of <username>
and <password>
to the MONGODB_URL
value.
app.js:
const path = require('path');
const express = require('express');
const session = require('express-session');
const sessionConfig = require('./config/session');
const db = require('./data/database');
const adminRoutes = require('./routes/admin/blog');
const authRoutes = require('./routes/admin/auth');
const defaultRoutes = require('./routes/home/default');
const postsRoutes = require('./routes/home/posts');
const quotationsRoutes = require('./routes/home/quotations');
const contactsRoutes = require('./routes/home/contacts');
const authMiddleware = require('./middlewares/auth-middleware');
const mongoDbSessionStore = sessionConfig.createSessionStore(session);
let port = 3000;
if (process.env.MONGODB_URL) {
port = process.env.MONGODB_URL;
}
const app = express();
app.set('views', [
path.join(__dirname, 'views/home'),
path.join(__dirname, 'views/admin')
]);
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use('/public/admin/images', express.static('public/admin/images'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(session(sessionConfig.createSessionConfig(mongoDbSessionStore)));
app.use(authMiddleware);
app.use('/', adminRoutes);
app.use('/', authRoutes);
app.use('/', defaultRoutes);
app.use('/', postsRoutes);
app.use('/', quotationsRoutes);
app.use('/', contactsRoutes);
app.use(function (req, res) {
res.status(404).render('404');
});
app.use(function (error, req, res, next) {
console.error(error);
res.status(500).render('500');
});
if (typeof(PhusionPassenger) !== 'undefined') {
PhusionPassenger.configure({ autoInstall: false });
}
if (typeof(PhusionPassenger) !== 'undefined') {
app.listen('passenger');
} else {
app.listen(3000);
}
I'm using o2switch as a hosting server. They use the Setup Node.js App tool, which deploy the website through Phushion Passenger.