Node.js Course

Table of Contents

Implemented a CRUD application for managing users using Node.js and Express.

Attention: Make sure your server is running.
use: node NODE/server.js to initialize the server.

Note: if you get an error of Cross-Origin Request, make sure your server is configured to allow CORS (Cross-Origin Resource Sharing).

you can install by using:
npm install cors

Lesson 01: Frontend and Backend Concepts

Lesson 02: Starting a Node.js Project

npm init

To start a Node.js project, you typically follow these steps:

  1. Initialize a new Node.js project using npm init.
      -> or skip the questions and use npm init -y
  2. Install necessary dependencies using npm install.
  3. Create your main application file (e.g., index.js) and start coding your server or application logic.

npm express

npm i express : Installs the Express.js framework, which is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

server.js

In a Node.js project, server.js is commonly used as the main entry point for the application. It typically contains the code to set up and start the server, define routes, and handle incoming requests.

to import modules in Node.js, you can use either the CommonJS syntax (require) or the ES6 module syntax (import).

CommonJS: const express = require('express');

ES6 Modules: import express from 'express';


Note: The ES6 module syntax is supported in Node.js starting from version 12 with the .mjs file extension
or by setting "type": "module" in the package.json.

Starting the Server

// Using ES6 Modules 
import express from 'express';
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
    res.send('Hello, World!');
});
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
    

HTML Methods

HTML methods

HTML Request Methods

HTTP Status Codes

Server Side

server.js Request Methods

server.js Response Methods

Starting the Server

// const express = require('express'); // CommonJS syntax

import express from 'express';
const app = express();
const PORT = 3000;

// see if the server is running
app.get('/', (req, res) => {
    res.send('Hello, World!');
});

// Start the server
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

On Terminal:

To start the server, you can use the following command in your terminal:

node NODE/server.js

Alternatively, you can use the following command to automatically restart the server whenever you make changes to the server.js file:

node --watch NODE/server.js

Params

Front-end

Query Params: Used to send data to the server as part of the URL, typically for filtering or searching.

Example: /users?name=John&age=30

Route Params: Used to capture values from the URL path, often for dynamic routing.

Example: /users/:id

Request Body: Used to send data to the server in the body of the request, often for creating or updating resources.

Example: { "name": "John", "age": 30 }

Back-end

Query Params: Using req.query.variable, gets the value of the query parameter. Example: /example?variable=value

Route Params: Accessed using req.params in Express.js, which returns an object containing the route parameters defined in the URL path.

Request Body: Accessed using req.body in Express.js, which contains the data sent in the body of the request. Note: To parse the request body, you need to use middleware like express.json() for JSON data or express.urlencoded() for URL-encoded data.

Object Notation

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is commonly used for transmitting data in web applications between a server and a client.

Toon

Toon (Token Object Notation) is a compact, human-readable encoding of the JSON data model that minimizes tokens and makes structure easy for models to follow. It's intended for LLM input as a drop-in, lossless representation of your existing JSON.