Node Express Crash Course Overview

Oct 7, 2024

Node Express Crash Course for Beginners

Overview

  • Duration: 2 hours
  • Build: Node Express project from scratch

Topics Covered

  1. Express Project Setup
  2. Creating an Express Server
  3. Installing Thunder Client for API Testing
  4. Setting Up Express Router
  5. Error Handling in Express
  6. Express Middleware Setup
  7. MongoDB Setup
  8. Using Mongoose Schema for Database Communication
  9. CRUD API Implementation
  10. User Authentication
  11. Controllers for Database Operations
  12. Password Hashing and Verification
  13. JWT Token for Authentication
  14. Handling Relationships in MongoDB Schema
  15. Protecting Routes for Authenticated Users
  16. User Authorization for accessing endpoints
  17. API Testing

Building the Contact Manager App

  • Create APIs to manage contacts
  • Introduce authentication and authorization concepts

RESTful API Conventions

  • CRUD Actions:
    • Get all contacts: GET /api/contacts
    • Get a single contact: GET /api/contacts/:id
    • Create contact: POST /api/contacts
    • Update contact: PUT /api/contacts/:id
    • Delete contact: DELETE /api/contacts/:id

Initial Setup

  • Install Node.js and Visual Studio Code
  • Initialize project with npm init
  • Create server.js for Express setup
  • Install necessary dependencies:
    • npm install express
    • npm install nodemon
    • npm install dotenv
    • npm install mongoose
    • npm install bcrypt
    • npm install jsonwebtoken
    • npm install express-async-handler

Express Server Creation

  • Setup basic Express server in server.js
  • Define the port using environment variables
  • Implement GET method for /api/contacts endpoint

Express Router

  • Create routes folder and contactRoutes.js file
  • Set up CRUD routes for contacts in the router

Error Handling

  • Implement custom error handling middleware
  • Handle validation errors and return JSON responses

Mongoose and MongoDB Setup

  • Setup MongoDB and create contacts collection
  • Create schemas using Mongoose for contacts
  • Implement CRUD operations in controllers

User Authentication

  • Implement registration and login endpoints
  • Use JWT for authentication
  • Hash passwords using bcrypt

Protecting Routes

  • Implement middleware to protect routes
  • Ensure only authenticated users can access certain endpoints

CRUD Operations

  • Implement CRUD operations in the contact controller
  • Ensure operations are tied to the authenticated user

Testing API

  • Use Thunder Client or Postman to test APIs
  • Validate responses and check for correct functionality

Conclusion

  • Build a fully functional contact manager application with authentication
  • Follow best practices for API design and security
  • Subscribe for future updates and tutorials.