Book Image

Learn Node.js by Building 6 Projects

By : Eduonix Learning Solutions
Book Image

Learn Node.js by Building 6 Projects

By: Eduonix Learning Solutions

Overview of this book

<p>With its event-driven architecture and efficient web services capabilities, more and more companies are building their entire infrastructure around Node.js. Node has become a de facto part of web development that any serious developer needs to master.</p> <p>This book includes six Node.js projects that gradually increase in complexity. You'll start by building a simple web server and create a basic website. You will then move to create the login system, blog system, chat system, and e-learning system.</p> <p>By creating and following the example projects in this book, you’ll improve your Node.js skills through practical working projects, and you'll learn how to use Node.js with many other useful technologies, such as ExpressJS, Kickstart, and Heroku.</p>
Table of Contents (12 chapters)

Pages routes and views


In the previous section we installed Express and we set up our basic app.js file and our package.json file. So we have one route above going to the Home page and all we're doing is just sending out some text inside an h1 tag.

We now want to load some Pug views for different routes in our application. But before we do that, we need to actually set up a couple of things.

Setting up View

Let's go to app.js and let's type in app.set. We need to tell Pug which folder the template files will be in, so we use views. Then as the second parameter, we'll use path.join and in there, we will put __dirname as the first parameter and the second parameter will be the folder name, views:

app.set('views', path.join(__dirname, 'views'));

We also need to set the view engine, so I'll use app.set('view engine') and then the second parameter will be pug. Alright, now instead of typing in res.send, we'll type in res.render and we'll render the index view:

app.set('view engine', 'pug');

app.use...