Skip to content
Open

test #86

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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.multiRootWorkspaceName": "node-express-course"
}
19 changes: 18 additions & 1 deletion 02-express-tutorial/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
console.log('Express Tutorial')
const express = require("express");
const app = express();
const port = 5000;
const people = require("./routes/people"); // import router from given folder
const auth = require("./routes/auth"); // import router from given folder

app.use(express.static("./methods-public")); // use these static files for the app
app.use(express.urlencoded({ extended: false })); // use urlencoder middleware to parse html body for data
app.use(express.json()); // this middleware parses data from JSON file type.

//controllers
app.use("/api/people", people); // set people router for /api/people url
app.use("/login", auth); //set login router for /login

//listen
app.listen(port, () => {
console.log(`listening on port ${port}`); // listen on predefined port
});
14 changes: 14 additions & 0 deletions 02-express-tutorial/authorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const authorize = (req, res, next) => {
const { user } = req.query; // read the user value from query
if (user) {
// if it has user data, set it as req.user so we can record and use it
req.user = user;
console.log(user);
next(); // dont forget this since it needs to read next function
} else {
// if user value is empty then give 401 unauthorized error
res.status(401).send("unauthorized entry"); // dont put next here since we dont want to send more data
// next(); // we are sending two res. sends in one request which gives error
}
};
module.exports = authorize;
71 changes: 71 additions & 0 deletions 02-express-tutorial/controllers/people.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { people } = require("../data"); // import people array to use it here

const getPeople = (req, res) => {
//GET
res.status(200).json({ success: true, data: people }); //send json object with success true and people array
};
const createPerson = (req, res) => {
//POST
const { name } = req.body; // destructure name property from the body.
if (name) {
res.status(201).json({ success: true, person: name }); // 201 is successfull post request. Send a Json file with new persons name
} else {
res.status(400).json({ success: false, msg: "please provide a name" }); // if form is empty, throw an error message in your req.body file.Bad request
}
};
const createPersonPostman = (req, res) => {
//POST on postman url
const { name } = req.body; // destructure name property from the body.
if (name) {
res.status(201).json({ success: true, data: [...people, { name: name }] }); // add name data to at the end of people array ... add to the end
} else {
res.status(400).json({ success: false, msg: "please provide a name" }); // if form is empty, throw an error message in your req.body file.Bad request
}
};
const updatePerson = (req, res) => {
//PUT
const { id } = req.params; //destruct id param from params
const { name } = req.body; //destruct name from body
const person = people.find((person) => person.id === Number(id)); //reference a person object from people array with given id

if (!person) {
//if a person with given id doesnt exist, throw an error
return res
.status(400)
.json({ success: false, msg: "please provide a valid id" }); // if id cant be found give error
}
const newPeople = people.map((person) => {
// if id exists, create a new people array since you cant edit them in js.
if (person.id === Number(id)) {
//Find person with given id in the array and change its name value to new one
person.name = name;
}

return person; // return new object with edited name
});
res.status(201).json({ success: true, data: newPeople }); // send newPeople instead of people since we changed the array
};

const deletePerson = (req, res) => {
//DELETE
const { id } = req.params; //destruct id param from params
const person = people.find((person) => person.id === Number(id)); //reference a person object from people array with given id

if (!person) {
//if a person with given id doesnt exist, throw an error
return res
.status(400)
.json({ success: false, msg: "Such id doesnt exist" }); // if id cant be found give error
}
const newPeople = people.filter((person) => person.id !== Number(id)); // filter a new array with elements not matching the given id
// how to delete array elements in js
res.status(201).json({ success: true, data: newPeople }); // send newPeople array with deleted element
};

module.exports = {
getPeople,
createPerson,
createPersonPostman,
updatePerson,
deletePerson,
}; // export and object with functions
10 changes: 10 additions & 0 deletions 02-express-tutorial/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//middleware works inbetween req and res
const logger = function (req, res, next) {
//express.js automatically links req res and next from http api
const method = req.method;
const url = req.url;
const date = new Date().getFullYear();
console.log(method, url, date);
next(); // go to next middleware, last one is res DONT FORGET THIS
};
module.exports = logger; // export this method
Loading