ROUTING Setting up Routing
Thu Mar 07 2024 06:23:17 GMT+0000 (Coordinated Universal Time)
Saved by @Marcelluki
Setting Up Routing You already know the benefits of modular code. As such, our routing logic should be divided into separate files. One file will be responsible for the body of the website, the second will take care of the content management system, and the third one will have code for the mobile app. Let's see how we can divide the following code into modules: // index.js // here, is the entry point setup const express = require('express'); const { PORT = 3000 } = process.env; const app = express(); // here we have data const users = [ { name: 'Jane', age: 22 }, { name: 'Hugo', age: 30 }, { name: 'Juliana', age: 48 }, { name: 'Vincent', age: 51 } ]; // here's where we'll do our routing app.get('/users/:id', (req, res) => { if (!users[req.params.id]) { res.send(`This user doesn't exist`); // it's important we don't forget to exit from the function return; } const { name, age } = users[req.params.id]; res.send(`User ${name}, ${age} years old`); }); app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); }); Save First things first, let's move our data into an individual file called db.js: // db.js module.exports = { users: [ { name: 'Jane', age: 22 }, { name: 'Hugo', age: 30 }, { name: 'Juliana', age: 48 }, { name: 'Vincent', age: 51 } ] }; Save Storing the data together with the configuration code looks messy and is generally considered bad practice in app development. Now, let's set up routing. Our routing logic should also be moved into an individual file. In this case, we'll need to write some more code. The response logic is described in the get() method's handler functions. In the code above, we were calling get() as a method of app. But there's no app variable in our new routing file. Further, since we can have only one app, we can't recreate this variable here. To take care of this, Express provides us with the Router() method, which creates a new router object. Once we create this object, we can attach our handlers to it, like so: // routes.js const router = require('express').Router(); // creating a router const { users } = require('./db.js'); // since this data is necessary for routing, // we need to import it router.get('/users/:id', (req, res) => { if (!users[req.params.id]) { res.send(`This user doesn't exist`); return; } const { name, age } = users[req.params.id]; res.send(`User ${name}, ${age} years old`); }); module.exports = router; // exporting the router Save Finally, let's set up our entry point inside the index.js file. To be able to use our routing, we should import the route file we just created into the index.js file. To execute the router, we need to call the use() method of the app. This method takes two parameters: The first part of the URL. The router will only start if a request begins with this line. The router itself, in our case, we've saved it as a const called router. // index.js const express = require('express'); const router = require('./routes.js'); // importing the router const { PORT = 3000 } = process.env; const app = express(); app.use('/', router); // starting it app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); }); Save We can make our code more modular by utilizing the first parameter of the use() method. We can create different routers for handling a number of routes: // index.js const express = require('express'); const router = require('./routes.js'); const api = require('./api.js'); const backoffice = require('./backoffice.js'); const { PORT = 3000 } = process.env; const app = express(); // different routers for different requests // looks awesome! app.use('/', router); app.use('/api', api); app.use('/admin', backoffice); app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); });
Comments