Learning Node.js from a Udemy course

My understanding

From what I understand, an instance of express() allows us access to the express router, which can be used to turn a website into small stand-alone components.

For example, unpuzzled could be the main website which could have multiple components such as a user feed of puzzle pieces, puzzle piece management, etc. These small components would be known as child apps of the parent app, unpuzzled. The child apps are created with an instance of the express router.

Main website which can have modularity with the help of express routers. Credit

A child app needs to be created as a pluggable module that can be included in the main express app. First we will create a folder called app, which will have all the modules that we will load into our main express app. Our current directory looks like this:

-chatcat
---app
------routes
---------index.js
------index.js
---node_modules
---public
---views
-package.json
-server.js

When we require a certain module, we usually require by the folder name because node.js automatically assumes that index.js within that folder will be called. We can change this by changing the configuration of the package.json file.

The routes object

Express Router: Mounting the routes part 2

In summary, we have a util module, in this case we call it a helper module, which contains a useful registerRoutes method. The registerRoutes method traverses the object and registers our routes. Once this is done,

Express Router: Handling a 404

Finally, I got to learn about what process means in node.js. ‘process’ means the file that was invoked using node command or nodemon, which in this case is server.js. Therefore, by using process.cwd, node.js gives us the ‘current working directory’ of the process, meaning the current working directory of server.js. Using this file path, we can get to the files in our views folder.

MongoLab

Keywords

Collections are like tables from traditional SQL database.

A document is like a record or a row of data in terms of SQL databases.

When a record is being created by our app, a collection is automatically created.

Environments and the config loader

When building web apps, you have to deal with a number of APIs and other configurable points such as a database access URI, or any other services. Due to several configurable points, we have to design our environments as efficiently as possible. There are two environments in Node.js

  1. Development Environment is when you are building your app locally and use the terminal to connect to services locally
  2. Production Environment is when you are deploying your app, to a service such as mongolab.

If we were to hard code all the security keys from each service into our local app then we would manually have to change it when deploying our app. This is very cumbersome and time consuming.

Configuration data when deployed should be saved on host’s environment variables, which can be created in a number of ways.

Mongoose and the database module

Mongoose is an object data mapper (ODM), which makes it easy for our node.js app to interact with our mongodb through the use of Mongoose’s high level functions. They help in simplifying our interaction.

Mongoose also brings structure to our interaction with mongodb, which is schemaless to begin with.

Sessions 101

HTTP is stateless, meaning when you send a request, it is unique. It has no relation to other requests. It can’t obtain the content of other requests to remember your credentials.

Sessions allow you to persist user data as a user goes through various webpages of a given website. Sessions are assigned to the data associated with a user, such as a user’s profile picture URI, or the user’s name, etc. This is done through a session ID which is stored in our server and set to expire after a given time.

Method 1

The session ID can be appended to every webpage link to make the given webpage aware of a particular user. This is shown in the red text. Once we append this information in a certain webpage’s link, our router will look up the session ID in our database to cross-reference the user’s credentials and permissions to the webpage in question.

Method 2

Session IDs can be stored in a cookie by our webserver in a user’s browser, which we can access whenever users request restricted pages. The session cookie is destroyed when the browser closes and clears its memory.

Implementing sessions using the express-session middleware

We will store our sessions data in mongolab, whereas the session ID will be stored as a session cookie in the user’s browser.

connect-mongo allows our middleware (express-session) to interact with mongodb.

We will now create a new module under our app folder which will be named session. In this module, we have an index.js file, which uses the require function to bring in express-session module and connect-mongo module with the parameter ‘session’, which is defined in line 2 when requiring express-session module.

The purpose of this is to allow connect-mongo access to configuration settings when initializing the session.

'use strict';
const session = require(‘express-session’);
const MongoStore = require(‘connect-mongo’)(session);

More to follow…


Social Authentication: An Introduction

Social Authentication: Introduction to Passport

  1. Site-A registers with Site-B, and obtains a Secret and an ID.
  2. When User tells Site-A to access Site-B, User is sent to Site-B where he tells Site-Bthat he would indeed like to give Site-A permissions to specific information.
  3. Site-B redirects User back to Site-A, along with an Authorization Code.
  4. Site-A then passes that Authorization Code along with its Secret back to Site-B in return for a Security Token.
  5. Site-A then makes requests to Site-B on behalf of User by bundling the Security Token along with requests.

Obtained from stackoverflow.

How does Node, Express, Connect and Middleware work together?

Everyauth vs passport.js by Jared Hanson

Authentication

For authentication, use Jared Hanson’s Passport.js, along with the following middlewares:

  1. connect-ensure-login: This middleware ensures that a user is logged in. If a request is received that is unauthenticated, the request will be redirected to a login page. The URL will be saved in the session, so the user can be conveniently returned to the page that was originally requested.

All middleware made with connect.js, which is a HTTP server framework. https://github.com/senchalabs/connect/wiki

File Uploads

Multer for file upload and two guides:

  1. First Guide
  2. Second guide

Conceptual Blocks

In this video — Express Router: Mounting the routes — 1 — At 5:43, line 26 — what is it’s purpose?

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.