Skip to content

Instantly share code, notes, and snippets.

@jasheloper
Last active January 13, 2020 15:26
Show Gist options
  • Save jasheloper/81c4a100240938dde038f02438635d57 to your computer and use it in GitHub Desktop.
Save jasheloper/81c4a100240938dde038f02438635d57 to your computer and use it in GitHub Desktop.
create a router by itself and call it in another file - express - backend
// inside /api/apiRoutes.js <- this can be place anywhere and called anything
const express = require('express');
// if the other routers are not nested inside /api then the paths would change
const userRoutes = require('./users/userRoutes');
const productRoutes = require('./products/productRoutes');
const clientRoutes = require('./clients/clientRoutes');
const router = express.Router(); // notice the Uppercase R
// this file will only be used when the route begins with "/api"
// so we can remove that from the URLs, so "/api/users" becomes simply "/users"
router.use('/users', userRoutes);
router.use('/products', productRoutes);
router.use('/clients', clientRoutes);
// .. and any other endpoint related to the users resource
// after the route has been fully configured, then we export it so it can be required where needed
module.exports = router; // it is recommended that this be the last line on the file
const express = require('express');
const apiRoutes = require('./api/apiRoutes');
const server = express();
server.use('/api', userRoutes);
server.listen(8000, () => console.log('API running on port 8000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment