JS-301-NodeServer
  • Introduction
  • js_library
    • Node Server
      • 00 - Intro
        • 01 - Purpose
        • 02 - Back-End Setup
        • 03 - Terms Cheat Sheet
      • 01 - Server Set up
        • 01 - npm packages
        • 02 - Express Intro
        • 03 - Express code
      • 02 - Development Tools
        • 01 - Nodemon Intro
        • 02 - Postman Intro
        • 03 - Postman set up
      • 03 - Routes Intro
        • 01 - Routes intro
        • 02 - Express Router() intro
        • 03 - Challenge 1
        • 04 - Challenge 2
      • 04 - Database Intro
        • 00 - DB Intro and Set up
          • 00 - DB Intro
          • 01 - PostgreSQL Intro
          • 02 - Install
        • 01 - Sequelize Intro
          • 01 - Sequelize intro
          • 02 - Initialize
      • 05 - Model View Controller
        • 01 - MVC
          • 00 - MVC Intro
        • 02 - Models
          • 01 - Intro to Models
          • 02 - Test Model
        • 03 - Controllers
          • 00 - Controllers Intro
          • 01 - Controller Set up
          • 02 - Create Method
          • 03 - req.body()
          • 04 - Crafting the Response
          • 05 - Sending the Response
          • 06 - JSON Response
          • 07 - Error Handling
        • 04 - Conclusion
      • 06 - Tokenization
        • 01 - JWT Intro
          • 01 - JWT intro
        • 02 - User Create
          • 01 - User Create
          • 02 - Refactor
        • 03 - User Token
          • 01 - JWT Package
          • 02 - Adding JWT
          • 03 - ENV
      • 07 - Encryption
        • 01 - bcrypt
        • 02 - bcrypt setup
      • 08 - Session
        • 00 - Session Intro
        • 01 - Sign In Method
        • 02 - Sign In Bcrypt
        • 03 - Sign In JWT
      • 09 - Middleware
        • 01 - Test Client HTML
        • 02 - Test Client JS
        • 03 - Middleware intro
        • 04 - Headers intro
        • 05 - Server Update
        • 06 - Test Post
        • 07 - Test Post Refactor
        • 08 - Post Data
        • 09 - Fetch From One
      • 10 - Authenticated Routes
        • 01 - Intro to Authenticated Routes
        • 02 - Validate Session
        • 03 - Changes to app.js
        • 04 - authtestcontroller.js
        • 05 - Delete an Item
        • 06 - Update an Item
        • 07 - Postman Testing
      • 11 - Authenticated Requests
        • 00 - Additions to index
        • 01 - Anatomy of a Request
        • 02 - Create User
        • 03 - Getting a Token
        • 04 - Get Items From One User
        • 05 - Creating an Item for a User
        • 06 - Get one item
        • 07 - Update an Item
        • 08 - Deleting an Item
        • 09 - Deleting with a Custom Event
      • 12 - Workout Log Server
        • 00 - Intro
      • 13 - More Sequelize Functions
        • Migrations
          • 00 - Intro
          • 01 - init and config
          • 02 - Creating the First Migration
          • 03 - Running Migrations
          • 04 - Reverting Migrations
          • 05 - Seeds
          • 06 - Reverting Seeds
        • Queries
          • 00 - Intro
          • 01 - Queries
Powered by GitBook
On this page
  • Orientation
  • What is a Dependency?
  • npm init
  • package.json
  • Project Dependencies
  1. js_library
  2. Node Server
  3. 01 - Server Set up

01 - npm packages

Previous01 - Server Set upNext02 - Express Intro

Last updated 7 years ago

In this module, we'll set up a few of the server packages (dependencies) that will be used in this application.

Orientation

What is a Dependency?

A dependency is a necessary requirement for a class or interface to use. Think of this analogy:

  • If you have a TV remote, you also need the batteries.

  • You cannot use the remote without also using those batteries.

  • Furthermore, you cannot continuously reuse that remote unless you also continuously reuse those batteries.

  • If your batteries fail, you can no longer use your remote.

Just like the remote depends on batteries. Your application will have other applications and frameworks (like Express) in the form of packages to rely on for it to run.

npm init

Let's initialize npm as our package manager for our dependencies. To initialize npm in the application, go through the following steps:

  1. Open the server folder.

  2. Open the command line window.

  3. Run npm init.

  4. Hit enter through all the prompts.

  5. You should see a new package.json file in your server folder.

package.json

  1. Take out the contents of the package.json file.

  2. Replace the contents with the code below.

  3. Run npm update. This will read all of the new dependencies from the package.json and load them all in the node_modules folder.

  4. Know that adding these new packages to package.json file allows us to freeze the particular versions of each dependency so that they harmonize together without breaking the application. For instance, let’s say that dependency bcrypt has changed to 3.0.0 and the new version does not work well with sequelize 4.0.0. Such a change might break our app.

{
  "name": "workoutlogserver",
  "version": "1.0.0",
  "description": "server for workout log app",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./node_modules/http-server/bin/http-server"
  },
  "author": "YOUR NAME HERE",
  "license": "MIT",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.2",
    "dotenv": "^5.0.1",
    "express": "^4.16.3",
    "jsonwebtoken": "^8.2.0",
    "pg": "^7.4.1",
    "pg-hstore": "^2.3.2",
    "sequelize": "^4.37.0"
  }
}

Project Dependencies

Here is another quick cheat sheet for the dependencies that will be used in this application.

Package

Purpose

bcrypt

Password-specific hashing that protects your password from being stored in plain text

body-parser

Parses the body of an HTTP request before it reaches an endpoint's functionality

dotenv

Allows developers to safely store configuration data

express

A web framework for Node.js that allows routing and processing HTTP requests

jsonwebtoken

A compact and self-contained way for securely transmitting information between parties

pg

A dependency for working with Postgres

pg-hstore

A dependency for working with Postgres for advanced functions with Postgres

sequelize

A tool that allows us to map models, pass data, and complete queries with a database

This just a quick overview of what these do. We'll go more in-depth on some of these packages as we build our server.

screenshot