Last active
January 13, 2020 15:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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