Full-Stack Web Development with Vue.js and Node

(singke) #1
Building an Express Application Chapter 2

Remember, there is no official naming convention in Node.js. We can definitely customize
the way we find simpler. We will discuss more about creating models in further chapters.


That will require us to create a connection with Mongo, which we will describe in further
chapters.


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}
Free download pdf