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)

Logout and access control


I should be currently logged in, so we'll create a route for our logout. Let's go to routes and then users.js. We'll go down to the bottom and say router.get, and the route is going to be /logout, which will be users/users/logout/. We'll pass in req and res and then all we need to do to logout is a req.logout. Then we'll create a message, we'll say req.flash, and that's going to be a success message and the text for that will just say You are now logged out. We want to simply redirect, so we need res.redirect. Now we'll redirect back to the Login page because the index page or the Members page we're not going to be able to access that. We'll put some restrictions on that, so this will go to/users/login:

router.get('/logout', function(req, res){
  req.logout();
  req.flash('success', 'You are now logged out');
  res.redirect('/users/login');
});

Let's save that and let's restart. Now the link you see goes to user's Logout. So if I click on that it brings us to theLogin...