Friday, 21 April 2017

express.js - single routing handler for multiple routes

I'm currently building an app and I have a routes file that looks like this.

const router = require('express').Router();

router.get('/', (req, res) => res.render('statics/home'));
router.get('/jobs', (req, res) => res.render('statics/jobs'));
router.get('/about-page', (req, res) => res.render('statics/about-page'));
router.get('/valves', (req, res) => res.render('statics/valves'));

router.all('*', (req, res) => res.notFound());

module.exports = router;

I am trying to figure out a way to refactor my routes and have a single route that accepts any string and then checks to see if a file exists matching it

Any help appreciated!



via Conor Heena

No comments:

Post a Comment