Book Image

Node.js 6.x Blueprints

By : Fernando Monteiro
Book Image

Node.js 6.x Blueprints

By: Fernando Monteiro

Overview of this book

Node.js is the most popular framework to create server-side applications today. Be it web, desktop, or mobile, Node.js comes to your rescue to create stunning real-time applications. Node.js 6.x Blueprints will teach you to build these types of projects in an easy-to-understand manner. The key to any Node.js project is a strong foundation on the concepts that will be a part of every project. The book will first teach you the MVC design pattern while developing a Twitter-like application using Express.js. In the next chapters, you will learn to create a website and applications such as streaming, photography, and a store locator using MongoDB, MySQL, and Firebase. Once you’re warmed up, we’ll move on to more complex projects such as a consumer feedback app, a real-time chat app, and a blog using Node.js with frameworks such as loopback.io and socket.io. Finally, we’ll explore front-end build processes, Docker, and continuous delivery. By the end of book, you will be comfortable working with Node.js applications and will know the best tools and frameworks to build highly scalable desktop and cloud applications.
Table of Contents (16 chapters)
Node.js 6.x Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Adding templates for login, sign-up, and profile


Now we have a solid basis to move forward with the project. At this time, we will add some template files for login, sign-up, and profile screens.

The expected result for these pages will be as shown in the following screenshot:

Login screen

Sign-up screen

Profile screen

  1. Now let's create the login template. Create a new file called login.ejs in the views folder and place the following code:

          <!DOCTYPE html> 
          <html> 
          <head> 
              <title><%= title %></title> 
              <% include ../partials/stylesheet %> 
          </head> 
          <body> 
            <% include ../partials/header %> 
       
            <div class="container"> 
                <% if (message.length > 0) { %> 
                    <div class="alert alert-warning alert-dismissible
                       fade in" role="alert"> 
                      <button type="button" class="close" data-dismiss=
                         "alert" aria-label="Close"> 
                        <span aria-hidden="true">&times;</span> 
                      </button> 
                      <strong>Ohps!</strong> <%= message %>. 
                    </div> 
                 <% } %> 
                <form class="form-signin" action="/login" method="post"> 
                <h2 class="form-signin-heading">Welcome sign in</h2> 
                <label for="inputEmail" class="sr-only">Email address</label> 
                <input type="email" id="email" name="email" class="form-
                  control" placeholder="Email address" required=""> 
                <label for="inputPassword" class="sr-only">Password</label> 
                <input type="password" id="password" name="password"
                 class="form-control" placeholder="Password" required=""> 
                <button class="btn btn-lg btn-primary btn-block" 
                   type="submit">Sign in</button> 
                <br> 
                <p>Don't have an account? <a href="/signup">Signup</a> 
                   ,it's free.</p> 
              </form> 
            </div> 
     
            <% include ../partials/footer %> 
            <% include ../partials/javascript %> 
          </body> 
          </html> 
    
  2. Add the login route to routes/index.js after the index route:

          /* GET login page. */ 
          router.get('/login', function(req, res, next) { 
              res.render('login', { title: 'Login Page', message:
               req.flash('loginMessage') }); 
           }); 
    

    Note

    In the template, we are making use of the connect-flash middleware to display error messages. Later, we will show how to install this component; don't worry right now.

  3. Let's add the signup template to the views/pages folder.

  4. Create a new file in views/pages and save as signup.ejs; then add the following code:

          <!DOCTYPE html>
          <html>
          <head>
            <title><%= title %></title>
            <% include ../partials/stylesheet %>
          </head>
          <body>
            <% include ../partials/header %>
            <div class="container">
              <% if (message.length > 0) { %>
                 <div class="alert alert-warning" role="alert">
                   <strong>Warning!</strong> <%= message %>.
                 </div>
               <% } %>
               <form class="form-signin" action="/signup" method="post">
                 <h2 class="form-signin-heading">Please signup</h2>
                 <label for="inputName" class="sr-only">Name address</label>
                 <input type="text" id="name" name="name" class="form-control"
                  placeholder="Name" required="">
                 <label for="inputEmail" class="sr-only">Email address</label>
                 <input type="email" id="email" name="email" class=
                   "form-control" placeholder="Email address" required="">
                 <label for="inputPassword" class="sr-only">Password</label>
                 <input type="password" id="password" name="password" 
                   class="form-control" placeholder="Password" required="">
                 <button class="btn btn-lg btn-primary btn-block" 
                   type="submit">Sign in</button> 
                  <br> 
                  <p>Don't have an account? <a href="/signup">Signup</a>
                      ,it's free.</p>
                </form>
              </div>
              <% include ../partials/footer %>
              <% include ../partials/javascript %>
            </body>
            </html>
  5. Now we need to add the route for the sign-up view. Open routes/index.js and add the following code right after login route:

          /* GET Signup */ 
          router.get('/signup', function(req, res) { 
              res.render('signup', { title: 'Signup Page', 
                 message:req.flash('signupMessage') }); 
          }); 
    
  6. Next, we will add the template to the profile page and the route to this page. Create a file called profile.ejs inside the view/pages folder and add this code:

          <!DOCTYPE html> 
          <html> 
          <head> 
              <title><%= title %></title> 
              <% include ../partials/stylesheet %> 
          </head> 
          <body> 
              <% include ../partials/header %> 
              <div class="container"> 
                <h1><%= title %></h1> 
                <div class="datails"> 
                  <div class="card text-xs-center"> 
                      <br> 
                    <img class="card-img-top" src="<%= avatar %>" 
                      alt="Card image cap"> 
                      <div class="card-block"> 
                        <h4 class="card-title">User Details</h4> 
                        <p class="card-text"> 
                            <strong>Name</strong>: <%= user.local.name %><br> 
                            <strong>Email</strong>: <%= user.local.email %> 
                        </p> 
                        <a href="/logout" class="btn btn-default">Logout</a> 
                      </div> 
                  </div> 
                </div> 
             </div> 
            <% include ../partials/footer %> 
            <% include ../partials/javascript %> 
          </body> 
          </html> 
    
  7. Now we need to add the route for the profile view; open routes/index.js and add the following code right after the signup route:

          /* GET Profile page. */ 
          router.get('/profile',  function(req, res, next) {
              res.render('profile', { title: 'Profile Page', user : req.user,
              avatar: gravatar.url(req.user.email ,  {s: '100', r: 'x', d:
               'retro'}, true) });
          }); 
    

    Tip

    We are using another middleware called gravatar; later, we will show how to install it.