diff --git a/01-node-tutorial/01-intro.js b/01-node-tutorial/01-intro.js index 8545876fc..d1b9ece86 100644 --- a/01-node-tutorial/01-intro.js +++ b/01-node-tutorial/01-intro.js @@ -1,9 +1,9 @@ -const amount = 9 +const amount = 9; if (amount < 10) { - console.log('small number') + console.log("small number"); } else { - console.log('large number') + console.log("large number"); } -console.log(`hey it's my first node app!!!`) +console.log(`hey it's my first node app!!!`); diff --git a/01-node-tutorial/app.js b/01-node-tutorial/app.js index 12898b321..652f4c085 100644 --- a/01-node-tutorial/app.js +++ b/01-node-tutorial/app.js @@ -1 +1,7 @@ -console.log('Welcome to Node Tutorial') +const { createReadStream } = require("fs"); + +const stream = createReadStream("./content/big.txt"); + +stream.on("data", (result) => { + console.log(result); +}); diff --git a/01-node-tutorial/package-lock.json b/01-node-tutorial/package-lock.json index 157df8ca2..5ad60caf5 100644 --- a/01-node-tutorial/package-lock.json +++ b/01-node-tutorial/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "tutorial", "version": "1.0.0", "license": "ISC", "dependencies": { diff --git a/02-express-tutorial/app.js b/02-express-tutorial/app.js index ce296a6ee..8840018d8 100644 --- a/02-express-tutorial/app.js +++ b/02-express-tutorial/app.js @@ -1 +1,14 @@ -console.log('Express Tutorial') +const express = require("express"); +const app = express(); +const people = require("./routes/people"); +const login = require("./routes/auth"); +app.use(express.static("./methods-public")); +app.use(express.urlencoded({ extended: false })); +app.use("/api/people", people); +app.use("/login", login); + +app.use(express.json()); + +app.listen(5000, () => { + console.log("listening on port 5000"); +}); diff --git a/02-express-tutorial/authorize.js b/02-express-tutorial/authorize.js new file mode 100644 index 000000000..9a13302d8 --- /dev/null +++ b/02-express-tutorial/authorize.js @@ -0,0 +1,11 @@ +const authorize = (req, res, next) => { + const { user } = req.query; + if (user === "jhon") { + req.user = { name: "jhon", id: 4 }; + next(); + } else { + res.status(401).send("invalid user"); + } +}; + +module.exports = authorize; diff --git a/02-express-tutorial/controllers/people.js b/02-express-tutorial/controllers/people.js new file mode 100644 index 000000000..1eb8042dc --- /dev/null +++ b/02-express-tutorial/controllers/people.js @@ -0,0 +1,38 @@ +let { people } = require("../data"); + +const getPeople = (req, res) => { + res.status(200).json({ success: true, data: people }); +}; + +const gePostMan = (req, res) => { + const { name } = req.body; + if (!name) { + res.status(401).json("Please provide the name").json({ error: true }); + } + res.status(200).json({ success: true, person: name }); +}; + +const updatePerson = (req, res) => { + const { id } = req.params; + const { name } = req.body; + const user_id = people.findIndex((p) => p.id == id); + people[user_id].name = "sammy james"; + res.status(201).json({ success: true, data: people }); +}; + +const deletePerson = (req, res) => { + const person = people.find((person) => person.id == Number(req.params.id)); + if (!person) { + return res.status(404).json({ error: "true", message: "person not found" }); + } + const updatedPeople = people.filter((p) => p.id !== person.id); + + res.status(200).json({ success: true, data: updatedPeople }); +}; + +module.exports = { + getPeople, + updatePerson, + deletePerson, + gePostMan, +}; diff --git a/02-express-tutorial/data.js b/02-express-tutorial/data.js index 08a64ff13..785db4192 100644 --- a/02-express-tutorial/data.js +++ b/02-express-tutorial/data.js @@ -1,42 +1,42 @@ const products = [ { id: 1, - name: 'albany sofa', + name: "albany sofa", image: - 'https://dl.airtable.com/.attachments/6ac7f7b55d505057317534722e5a9f03/9183491e/product-3.jpg', + "https://dl.airtable.com/.attachments/6ac7f7b55d505057317534722e5a9f03/9183491e/product-3.jpg", price: 39.95, desc: `I'm baby direct trade farm-to-table hell of, YOLO readymade raw denim venmo whatever organic gluten-free kitsch schlitz irony af flexitarian.`, }, { id: 2, - name: 'entertainment center', + name: "entertainment center", image: - 'https://dl.airtable.com/.attachments/da5e17fd71f50578d525dd5f596e407e/d5e88ac8/product-2.jpg', + "https://dl.airtable.com/.attachments/da5e17fd71f50578d525dd5f596e407e/d5e88ac8/product-2.jpg", price: 29.98, desc: `I'm baby direct trade farm-to-table hell of, YOLO readymade raw denim venmo whatever organic gluten-free kitsch schlitz irony af flexitarian.`, }, { id: 3, - name: 'albany sectional', + name: "albany sectional", image: - 'https://dl.airtable.com/.attachments/05ecddf7ac8d581ecc3f7922415e7907/a4242abc/product-1.jpeg', + "https://dl.airtable.com/.attachments/05ecddf7ac8d581ecc3f7922415e7907/a4242abc/product-1.jpeg", price: 10.99, desc: `I'm baby direct trade farm-to-table hell of, YOLO readymade raw denim venmo whatever organic gluten-free kitsch schlitz irony af flexitarian.`, }, { id: 4, - name: 'leather sofa', + name: "leather sofa", image: - 'https://dl.airtable.com/.attachments/3245c726ee77d73702ba8c3310639727/f000842b/product-5.jpg', + "https://dl.airtable.com/.attachments/3245c726ee77d73702ba8c3310639727/f000842b/product-5.jpg", price: 9.99, desc: `I'm baby direct trade farm-to-table hell of, YOLO readymade raw denim venmo whatever organic gluten-free kitsch schlitz irony af flexitarian.`, }, -] +]; const people = [ - { id: 1, name: 'john' }, - { id: 2, name: 'peter' }, - { id: 3, name: 'susan' }, - { id: 4, name: 'anna' }, - { id: 5, name: 'emma' }, -] -module.exports = { products, people } + { id: 1, name: "john" }, + { id: 2, name: "peter" }, + { id: 3, name: "susan" }, + { id: 4, name: "anna" }, + { id: 5, name: "emma" }, +]; +module.exports = { products, people }; diff --git a/02-express-tutorial/logger.js b/02-express-tutorial/logger.js new file mode 100644 index 000000000..5c771562f --- /dev/null +++ b/02-express-tutorial/logger.js @@ -0,0 +1,16 @@ +///MIDDLE WARE, consider it as a helper function +///so that you don't have to keep repeating the same code +//again and again. + +// the function logger is a middleware, +// We don't need to pass the req,res parameters when calling the function +const logger = (req, res, next) => { + const method = req.method; + const time = new Date().getFullYear(); + const path = req.url; + console.log(method, time, path); + next(); + /// you need to pass or return something when using middleware other wise it will left hanging +}; + +module.exports = logger; diff --git a/02-express-tutorial/methods-public/javascript.html b/02-express-tutorial/methods-public/javascript.html index 25ec4f1b4..3bbbd915e 100644 --- a/02-express-tutorial/methods-public/javascript.html +++ b/02-express-tutorial/methods-public/javascript.html @@ -44,40 +44,40 @@