In this tutorial, we create a basic Node JS site with the Express framework from scratch. The project is meant to serve as a generic site that can be extended for use in most consumer-facing applications. The site incorporates much of the core functionality expected in modern apps today:
Database integration with Mongo DB
REST API design
This is a good introduction to Node JS and backend programming in general. It provides a soid foundation for the more advanced, full-featured tutorials on Velocity 360..
Setting Up With Express
In this video, we install the required tools (Node, express, Mongo) and create a basic web project using the Express Generator. Then we add our first route, the API handler, which will eventually manage the main CRUD (Create, Read, Update, Delete) operations for our profiles.
Mongo DB and API Routes
In this video, we set up the Mongo database integration using the Mongoose ORM then add the Profile schema. We connect to Mongo and run queries for all Profile objects in the database (none right now) via the API route from the first video.
In this video, we create a simple sign up for on the home page in order to create actual profiles for the backend. We register a few fake profiles and then create another route to fetch them individually.
Controllers and Promises
In this video, we extract the Profile CRUD operations into its own controller file in order the make the API route more modular. We also introduce the concept of promises to avoid overly nested callbacks and use promises instead of completion handlers to run our controller code.
In this video, we conceal passwords from the API requests. Then we hash the passwords on profile registrations so that even if they are exposed from the database, they will be encrypted. Last, we set up an account route for the registration process which will redirect the user to a profile page on sign up.
We set up session management in this video in order to keep track of the logged in user. This way, whenever the user navigates to various pages on the site or leaves and returns later, we will know who the user is.
JSON Web Tokens
In this video we use JSON web tokens to further enhance our authentication set up. When a user logs in, we create a token with secret key that gets passed back and forth with every subsequent request. We then use it to identify the logged in user on the profile page and render the username.
In this video, we set up the comment area so that users can submit comments. We post comments to your API handler at first to ensure it works, then we create a new endpoint in the account route in order to bind comments to the profile that is currently logged in.
Displaying Profile Comments
In this video, we render the comments for the logged in user on the profile page. We changed the template engine to mustache which gives us additional functionality for looping through data.
Styling and Partials
To finish out the site, we add Bootstrap for basic styling on the profile page. We also render the Nav bar using partials which enables us to avoid repeat code throughout the various pages of a site.
Join for free today and deploy your site in seconds