I am using express and i want to redirect to 404 when the page is not found. Somehow it is always routing to /dashboard
when I insert the invalid URL in browser. Not sure How to add the check fusing wildcard operation
I have tried *, /**, /*
Here is my code which goes in index-routes.js
file
//Base routes
router.get('/*', async (req, res, next) => {
const data = {
title: 'TruVoice',
appVersion: req.appVersion || '',
name: secure.session.name || '',
email: secure.session.email || '',
userId: '',
module: secure.session.homeModule,
appPath: secure.session.homeModuleUrl,
sessionAge: env.sessionAge,
isDev: env.isDevelopment,
truvoiceUrl: env.truvoiceUrl,
truvoiceApiUrl: env.truvoiceApiUrl,
froalaKey: env.froalaKey,
pusherKey: env.pusherKey,
pusherCluster: env.pusherCluster,
nonce: res.locals.nonce,
};
if (req.path === '/') {
const account = new Account(req.db);
const user_settings = await account.getUserSettings(
new ObjectId(session.userId)
);
data.homeUrl = user_settings.homeUrl || '/dashboard';
}
res.render('index', data);
});
Now i want to update code to something like this
Now i have tried this in my app.js file where I injected routes and want to route to 404
on invalid routes. I adding app.all
before my index routes and it was redirecting all pages to 404
app.use('/', index_routes);
app.all('*', function(req, res) {
res.status(404).json({
success: false,
data: '404'
})
});
What should I try here?