Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overhaul with template #67

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 4 additions & 4 deletions 01-node-tutorial/01-intro.js
Original file line number Diff line number Diff line change
@@ -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!!!`);
8 changes: 7 additions & 1 deletion 01-node-tutorial/app.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions 01-node-tutorial/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion 02-express-tutorial/app.js
Original file line number Diff line number Diff line change
@@ -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");
});
11 changes: 11 additions & 0 deletions 02-express-tutorial/authorize.js
Original file line number Diff line number Diff line change
@@ -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;
38 changes: 38 additions & 0 deletions 02-express-tutorial/controllers/people.js
Original file line number Diff line number Diff line change
@@ -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,
};
32 changes: 16 additions & 16 deletions 02-express-tutorial/data.js
Original file line number Diff line number Diff line change
@@ -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 };
16 changes: 16 additions & 0 deletions 02-express-tutorial/logger.js
Original file line number Diff line number Diff line change
@@ -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;
42 changes: 21 additions & 21 deletions 02-express-tutorial/methods-public/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,40 @@ <h3>Javascript Form</h3>
crossorigin="anonymous"
></script>
<script>
const result = document.querySelector('.result')
const result = document.querySelector(".result");

const fetchPeople = async () => {
try {
const { data } = await axios.get('/api/people')
const { data } = await axios.get("/api/people");

const people = data.data.map((person) => {
return `<h5>${person.name}</h5>`
})
result.innerHTML = people.join('')
return `<h5>${person.name}</h5>`;
});
result.innerHTML = people.join("");
} catch (error) {
result.innerHTML = `<div class="alert alert-danger">Can't Fetch Data</div>`
result.innerHTML = `<div class="alert alert-danger">Can't Fetch Data</div>`;
}
}
fetchPeople()
};
fetchPeople();
// submit form
const btn = document.querySelector('.submit-btn')
const input = document.querySelector('.form-input')
const formAlert = document.querySelector('.form-alert')
btn.addEventListener('click', async (e) => {
e.preventDefault()
const nameValue = input.value
const btn = document.querySelector(".submit-btn");
const input = document.querySelector(".form-input");
const formAlert = document.querySelector(".form-alert");
btn.addEventListener("click", async (e) => {
e.preventDefault();
const nameValue = input.value;

try {
const { data } = await axios.post('/api/people', { name: nameValue })
const h5 = document.createElement('h5')
h5.textContent = data.person
result.appendChild(h5)
const { data } = await axios.post("/api/people", { name: nameValue });
const h5 = document.createElement("h5");
h5.textContent = data.person;
result.appendChild(h5);
} catch (error) {
// console.log(error.response)
formAlert.textContent = error.response.data.msg
formAlert.textContent = error.response.data.msg;
}
input.value = ''
})
input.value = "";
});
</script>
</body>
</html>
Loading