I am new to express/node and apologize in advance for my inexperience/ignorance. I have written an express app that runs perfectly on my local system. Our hosting service uses Phusion Passenger and my app is having all sorts of routing issues now. At first I got the "Cannot GET" URL problem, but research allowed me to figure out what might be the problem with the routing.
After adding my app name, 'scoreapp,' to the route, I can get to the home page, but all other routes fail and also the code cannot find the css files. It does, however, find the views and other supporting files. Seems it is just the routes that are at issue. I have tried multiple solutions... taking the appname out of the router in app.js and putting it in the individual routes... Nothing seems to work.
The app fails with an internal server error, doesn't write in the log. Just dies. I am at a loss as to how to even try to troubleshoot. Any assistance is welcomed or even documentation that uses more than the very simplest case of having all routes in app.js.
All of the documentation I can find uses only app.js, but my code uses routers. The following is an excerpt from the structure of my app:
/bin
/node_modules
/public
/routes (express routes)
/auth.js
/competition.js
/competitors.js
...
/services (access the mysql db)
/auth.js
/common.js
/competition.js
...
/views (all the html code as ejs files)
/auth-login.ejs
...
/competition-mainPage.ejs
...
/competitors-manage.ejs
...
app.js
config.js (db pooling and access)
package.json
package-lock.json
README.md
The following code, from my app.js, shows the addition of the app name to the route path:
//excerpt from app.js
...
var indexRouter = require('./routes/index');
var judgesRouter = require('./routes/judges');
var divisionsRouter = require('./routes/divisions');
var competitorsRouter = require('./routes/competitors');
var entriesRouter = require('./routes/entries');
var competitionRouter = require('./routes/competition');
var scoresRouter = require('./routes/scores');
var authRouter = require('./routes/auth');
var messageRouter = require('./routes/message');
...
app.use('/scoreapp', indexRouter);
app.use('/scoreapp/auth', authRouter);
app.use('/scoreapp/judges', judgesRouter);
app.use('/scoreapp/divisions', divisionsRouter);
app.use('/scoreapp/competitors', competitorsRouter);
app.use('/scoreapp/entries', entriesRouter);
app.use('/scoreapp/competition', competitionRouter);
app.use('/scoreapp/scores', scoresRouter);
app.use('/scoreapp/message', messageRouter);
...
Access to the root of my app (/scoreapp) works fine. It finds the route in index.js, but it cannot find any other route. The following is the /routes/index.js file:
//index.js
const express = require('express');
const router = express.Router();
const common = require('../services/common');
//* display login page //
router.get('/', function(req, res, next){
//check login status
//if (!common.checkSession(req.cookies)) {
// res.redirect('/scoreapp/auth/login');
//}
res.render('competition-mainPage', {title: 'KMP Competitions'});
return;
})
module.exports = router;
At the moment I have commented out the redirect to the login page, as the app cannot find it on my hosting server. However, it finds the view, 'competition-mainPage', just fine.
The following is an excerpt from auth.js, where it should find /auth/login:
//excerpt from auth.js
const express = require('express');
const router = express.Router();
const { check, validationResult } = require('express-validator');
const common = require('../services/common');
const auth = require('../services/auth');
//display login page
router.get('/login', async function(req, res, next){
try{
res.render('auth-login', {title: 'Login'});
} catch(err) {
console.error(`Error while displaying login screen`, err.message);
next(err);
}
})
router.get('/logout', async function(req, res, next){
try{
res.render('auth-logout', {title: 'Logout'});
} catch(err) {
console.error(`Error while displaying logout screen `, err.message);
next(err);
}
})
...