Creating view files for the Express.js application
We learned about how to create controllers
in the last section. In this section, we will talk about how to add and customize view files. If you remember, we have this code in controllers/users.js
:
module.exports.controller = (app) => { // get users page app.get('/users', (req, res) => { res.render('index', { title: 'Users' }); }) }
Let's change a line that renders the index
file to this:
module.exports.controller = (app) => {
// get users page
app.get('/users', (req, res) => {
res.render('users', { title: 'Users' });
})
}
This means that the controller wants to load the users
file, which is in the views
folder. Let's go ahead and create a users.pug
file in the views
folder.
After creating the file, paste in the following code; this is the same code as in the index.pug
file in our views
folder:
extends layout block content h1= title p Welcome to #{title}
Now, if we used nodemon
, we don't have to restart our server...