📚

Express.js Tutorial Series Notes

Jul 14, 2024

Express.js Tutorial Series

Introduction

  • Presenter: Ansen
  • Purpose: Learn Express.js in 2022 through a new series of tutorials.
  • Previous Content: Tutorial on Express.js made roughly two years ago.

What is Express.js?

  • Definition: A web framework for building server-side applications.
  • **Uses: **
    • Building APIs for clients
    • Performing server-side tasks such as saving data to a database
    • Scheduling cron jobs
    • Building real-time applications (e.g., with Socket.io)
    • Versatile; can be used for literally anything on the server side

Typical Uses in Web Applications

  • Creating/Persisting Users
  • Keeping track of user invoices
  • Performing tasks that need to be executed server-side
  • Easier server-side application building with Express.js

Tutorial Series Content

  • Start from basics
  • Setting up a simple Express app
  • Basics of GET and POST requests
  • How to:
    • Retrieve data from Express application
    • Create data with Express application
    • Connect to databases (MySQL and MongoDB)
    • Use an ORM (Object Relational Mapper)
  • Advanced topics:
    • Sessions
    • Authentication
    • More complex concepts

Prerequisites

  • Basic knowledge of JavaScript
  • Familiarity with Node.js
  • Have Node.js installed
    • Download: nodejs.org
    • Current Version Used: 16.13.1 (or newer)
  • Text editor (e.g., Visual Studio Code)
  • HTTP Client (e.g., Postman)
    • Alternative: Any other HTTP client

Setting Up the Project

  • Terminal Setup: Windows PowerShell (or Terminal/iTerm for Mac/Linux)
  • Steps:
    1. Create a new directory: express-js-tutorial
    2. Navigate into the directory: cd express-js-tutorial
    3. Initialize npm: npm init -y
    4. Install Express.js: npm i express
    5. Open Visual Studio Code: code .

Optional but Recommended

  • Install nodemon locally:
    • Ensures nodemon runs locally even if not installed globally
    • Sets up some scripts for development

Project Structure

  • Create a src folder
  • Create index.js in src
  • Update package.json with scripts: "scripts": { "start": "node src/index.js", "start:dev": "nodemon src/index.js" }
  • Purpose of nodemon:
    • Restarts the application upon changes
    • Automates the process of restarting the app

Basic Express App Setup

  • Import Express:

    const express = require('express');
  • Create an Express app instance:

    const app = express();
  • Set the port:

    const port = 3001;
  • Start the server:

    app.listen(port, () => { console.log(`Running express server on port ${port}`); });
  • Running the Application:

    1. Run start script: npm start or npm run start:dev
    2. Verify by visiting: http://localhost:3001
  • Issue: "Cannot GET /"

    • Happens because no routes are defined yet

Next Steps

  • Focus on GET requests
  • Explanation and implementation of GET requests in Express.js

Conclusion

  • Next Episode Preview: Setting up a simple GET request
  • Closing: Thank you for watching, see you in the next episode. Peace out.