Tag Archives: node.js

01 Project – Node.js / Express / MongoDB – Part 13 – Creating the controller and the view model for the image page

The controller for the image will handle uploading and saving of the image files via the form on the homepage.

In the image controller we are going to build a view model for a single image. It will render a detailed view of an image (including the comments for the image).

Continue reading

01 Project – Node.js / Express / MongoDB – Part 10 – Creating {{ timestamp }} global helper

The global helper that we are going to create will be used to format a timestamp. It will display a date according to how long ago the event occurred. We will use this for things such as comments and image timestamps.

First, we need to update our server/configure.js module. We are going to add a section to define our helpers:

app.engine('handlebars', exphbs.create({
    defaultLayout: 'main',
    layoutsDir: app.get('views') + '/layouts',
    partialsDir: [app.get('views') + '/partials'],
    helpers: {
      timeago: function(timestamp) {
        return moment(timestamp).startOf('minute').fromNow();
      }
    }
  }).engine);

Continue reading

01 Project – Node.js / Express / MongoDB – Part 09 – Creating Partials

Partials are small views that we can insert in any part of our web page. Using partials is a good practice in web development since partials reduce code duplication and create reusable components.

For our application, we’re using partials for the sidebar in the main layout. With every view’s viewModel , we will include a JavaScript object called sidebar that will contain the data specifically for the stats, popular images, and recent comments found within the sidebar partial.

Continue reading

01 Project – Node.js / Express / MongoDB – Part 08 – Creating the main.handlebars file layout

When you build a website you would want to have a layout (template) used by every page. This layout should contain a header with title, logo etc, a navigation bar (if the developer needs a navigation bar), and a footer to contain the copyright information and maybe other information. Handlebars helps you with this matter!

Let’s create a layout file for our application by creating a new file named main.handlebars within the views/layouts folder. Insert the following code in it:

<!DOCTYPE HTML>
<html lang="en">
<head>
  <title>Project2Img</title>

   Continue reading 

01 Project – Node.js / Express / MongoDB – Part 07 – Creating the second view: image.handlebars file

Let’s create the second view file. Under the views folder, create a new file and name it image.handlebars. Copy the following code into the image.handlebars file.

<div class="panel panel-primary">
  <div class="panel-heading">
    <h2 class="panel-title">{{ image.title }}</h2>
  </div>
  <div class="panel-body">
    <p>{{ image.description }}</p>

     Continue reading 

01 Project – Node.js / Express / MongoDB – Part 06 – Creating the first view: index.handlebars file

A view is a HTML page. It is called view because of the MVC (Model-View-Controller) design pattern. A model is the data that is displayed on a page, the view is the page itself, and the controller is what communicates between the model and the view.

We are going to create two views: index.handlebars as a home page and image.handlebars for the image page.

Continue reading

01 Project – Node.js / Express / MongoDB – Part 02 – Organizing project folders and installing the dependencies

Before starting to write code you have to set up a project folder with the proper folder structure where all your files will reside. My project folder structure is shown below.

ProjectOne
***helpers
***controllers
***public
******css
******img
******js
******js
******upload
***server
***views
******layouts
******partials

Continue reading

Building an e-Commerce website with Node – Part 26 – Edit profile page in Node JS

In this post I will show you how to add an edit profile page feature to your Node.js web application.

First, we need to create two routes, both of them with the same URL, one is GET and another one is POST. Open routes/user.js and at the end of the file, just above the module.exports = router;, let’s create the routes.


router.get('/edit-profile', function(req, res, next) {
  res.render('accounts/edit-profile', { message: req.flash('success')});
});

router.post('/edit-profile', function(req, res, next) {
  User.findOne({ _id: req.user._id }, function(err, user) {

    if (err) return next(err);

    if (req.body.name) user.profile.name = req.body.name;
    if (req.body.address) user.address = req.body.address;

    user.save(function(err) {
      if (err) return next(err);
      req.flash('success', 'Successfully Edited your profile');
      return res.redirect('/edit-profile');
    });
  });
});

Continue reading

Node JS Introduction

About Node.js

Node.js is runtime environment for JavaScript. That means you can write JavaScript code wherever Node.js is installed. In other words, it is JavaScript on the server-side but not only.

Node.js is intended to be used for building web applications that have to handle much web traffic. That’s because multiple requests client-server are run on a single threat which means that it provides more scalability and performance for any web application written with Node.js. For example, Node.js is suitable for building real-time web applications such as chat rooms, collaboration tools, online games, etc.

Continue reading