// monsters.js const express = require('express'); const monstersRouter = express.Router(); const monsters = { '1': { name: 'godzilla', age: 250000000 }, '2': { Name: 'manticore', age: 21 } } monstersRouter.get('/:id', (req, res, next) => { const monster = monsters[req.params.id]; if (monster) { res.send(monster); } else { res.status(404).send(); } }); module.exports = monstersRouter; //// and // main.js const express = require('express'); const app = express(); const monstersRouter = require('./monsters.js'); app.use('/monsters', monstersRouter);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter