Docsity

NodeJS and ExpressJS Tutorial: Building REST APIs, Lecture notes of Italian Language

Partial preview of the text

Download NodeJS and ExpressJS Tutorial: Building REST APIs and more Lecture notes Italian Language in PDF only on Docsity!

Introduction to NodeJS

Instructor: Nguyễn Trung Nghĩa

REST API VS Web Services

Tools

Installation

Node.js — Download Node.js®

(nodejs.org)

Create project

Install packages npm i mysql2 express npm i D nodemon

Create Server import express from "express" // import ExpressJS framreword const app = express(); // use the expressJS const PORT = process.env.POST || 5000 ; // define PORT app.listen(PORT, () => console.log("Server Open At port: ", PORT));

Create GET request import express from "express" // const app = express(); // use the expressJS const PORT = process.env.POST || 5000 ; // define PORT app.listen(PORT, () => console.log("Server Open At port: ", PORT)); app.get("/", (req, res, next) => { console.log(req.method) res.send("Hello"); // send response }); // create GET request

Work with Routes

Create Routes & Handlers export const getAllProducts = async (req, res) => { return res.send("GET ALL PRODUCTS"); }; import { Router } from "express"; import { getAllProducts } from "../handlers/index.js"; const appRouter = Router(); appRouter.get("/", getAllProducts) export default appRouter;

Time to test

So, what happen after all?

Let’s complete Handlers export const getAllProducts = async (req, res) => { return res.send("GET ALL PRODUCTS"); }; export const getProduct = async (req, res) => {}; export const createProduct = async (req, res) => {}; export const updateProduct = async (req, res) => {}; export const deleteProuct = async (req, res) => {};

V. Working with DB

If you use MAC run mySQL by docker