





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to building rest apis using nodejs and expressjs. It covers installation, creating get and post requests, working with routers and handlers, and connecting to a mysql database. The guide includes code snippets and instructions for setting up a development environment and testing api endpoints with postman. It also references external resources for further learning, making it a practical resource for web development students. Useful for understanding the basics of creating a rest api using node.js and express, providing a step-by-step guide to setting up the environment, creating routes, and connecting to a database. It includes practical examples and references to external resources, making it a valuable resource for beginners in web development.
Typology: Lecture notes
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















REST API VS Web Services
Installation
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
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