Routing

Listen GET requests on path /hello:

app.get('/hello', (req, res) => {
  window.alert('Hello World');
});

Listen POST requests on path /item:

app.post('/item', (req, res) => {
  window.alert('Got a POST request at /item');
});

Listen GET requests on path starting with /api/:

app.get(/^api\//, (req, res) => {
  console.log(`api was requested ${req.uri}`);
});

Get parameters from path

app.get('/product/:id', (req, res) => {
  // if we have /product/42 then
  // req.params.id = 42
});
app.get('/user/:firstname?/:lastname', (req, res) => {
  // if we have /user/camel/aissani then
  // req.params.firstname = 'camel'
  // req.params.lastname = 'aissani'

  // if we have /user/aissani then
  // req.params.firstname = undefined
  // req.params.lastname = 'aissani'
});
app.get('/user/:id', (req, res) => {
  // if we have /user/1,2,3 then
  // req.params.id = [1,2,3]
});

You can have the full capabilities of Express-style path with this plugin frontexpress-path-to-regexp


Use the frontexpress.Router class to create modular, mountable route handlers.

Create a router file named birds.js:

import frontexpress from 'frontexpress';

const router = frontexpress.Router();

// specific middleware for this router
router.use((req, res, next) => {
  console.log(`Time: ${Date.now()}`);
  next();
});

// listen navigation on the home page
router.get('/', (req, res) => {
  document.querySelector('.content').innerHTML = '<p>Birds home page</p>';
});

// listen navigation on the about page
router.get('/about', (req, res) => {
  document.querySelector('.content').innerHTML = '<p>About birds</p>';
});

export default router;

Then, load the router module in the app:

import birds  from './birds';
...
app.use('/birds', birds);