Skip to content
Snippets Groups Projects
Commit 90ef25fc authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

adding week2 examples

parent 3e682fcc
No related branches found
No related tags found
No related merge requests found
Showing
with 5750 additions and 0 deletions
# Info
run npm i to install necessary modules
\ No newline at end of file
const express = require("express");
const app = express();
const cookieParser = require("cookie-parser");
const portNumber = 3000;
app.use(cookieParser());
app.get("/", (request, response) => {
response.cookie("campusLocation", "College Park", { httpOnly: true });
response.send("We set a cookie named campusLocation with a value of College Park");
});
app.get("/setMascotCookie", (request, response) => {
response.cookie("mascot", "testudo", { httpOnly: true });
response.send("We set a cookie named mascot that has the value testudo");
});
app.get("/check", (request, response) => {
console.log(request.cookies.mascot);
response.send(`Value of mascot cookie: ${request.cookies.mascot}`);
});
console.log(`Server listening on port ${portNumber}`);
const homeURL = `http://localhost:${portNumber}`;
console.log(homeURL);
console.log(`${homeURL}/setMascotCookie`);
console.log(`${homeURL}/check`);
app.listen(portNumber);
This diff is collapsed.
{
"name": "cookies",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"cookie-parser": "^1.4.6",
"express": "^4.18.1",
"nodemon": "^3.1.0"
}
}
# Info
In VS Code do not open the folder RouterCookiesSessionsCode;
open the Cookies, Router or Sessions folder to run the particular
example.
\ No newline at end of file
# Info
run npm i to install necessary modules
\ No newline at end of file
const express = require("express");
const app = express();
const portNumber = 3000;
const buildings = require("./routes/buildings");
const schoolDorms = require("./routes/schoolDorms");
/* Treating like middleware. Use buildings.js file to handle
endpoints that start with /buildings */
app.use("/buildings", buildings);
/* Treating like middleware. Use schoolDorms.js file to handle
endpoints that start with /dorms. Examples shows you
don't have to name file after part of the end point */
app.use("/dorms", schoolDorms);
app.use("/", (request, response) => {
response.send("/ in app.js ");
});
console.log(`Server listening on port ${portNumber}`)
const homeURL = `http://localhost:${portNumber}`;
console.log(homeURL);
/* buildings URLs */
const buildingsURL = `${homeURL}/buildings`;
const buildingsIribeURL = `${homeURL}/buildings/iribe`;
console.log(buildingsURL);
console.log(buildingsIribeURL);
/* dorms URLs */
const dormsURL = `${homeURL}/dorms`;
const dormsLaPlataURL = `${homeURL}/dorms/laplata`;
console.log(dormsURL);
console.log(dormsLaPlataURL);
app.listen(portNumber);
\ No newline at end of file
This diff is collapsed.
{
"name": "routerscookies",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"express-router": "^0.0.1",
"nodemon": "^2.0.16"
}
}
const express = require('express');
const router = express.Router();
/* router is like a mini application
endpoint that processes anything that
starts with /building
*/
/* We can add our own middleware that only
applies to buildings */
// http://localhost:3000/buildings/
router.get("/", (request, response) => {
response.send("/ in building.js")
});
// http://localhost:3000/buildings/iribe
router.get("/iribe", (request, response) => {
response.send("/iribe in building.js")
});
// http://localhost:3000/buildings/notValid
router.use((request, response) => {
response.status(404).send("Resource Not Found (in building router)");
});
module.exports = router;
\ No newline at end of file
const express = require('express');
const router = express.Router();
/* router is like a mini application
endpoint that processes anything that
starts with /dorms
*/
/* We can add our own middleware that only
applies to dorms */
// http://localhost:3000/dorms
router.get("/", (request, response) => {
response.send("/ in dorms.js")
});
http://localhost:3000/dorms/laplata
router.get("/laplata", (request, response) => {
response.send("/laplata in dorms.js")
});
module.exports = router;
\ No newline at end of file
# Info
run npm i to install necessary modules
\ No newline at end of file
const express = require("express");
const app = express();
const session = require("express-session");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const portNumber = 3000;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
session({
resave: true,
saveUninitialized: false,
secret: "putsomethingsecretheredontshow", // use .env for secret string
})
);
app.post("/login", (request, response) => {
let message;
/*
* Do not put passwords like we are doing; this is just to practice sessions.
* We are allowing two users: peter (password terps) and mary (password testudo)
* This will allow us to simultaneously have two clients (use insomnia for one
* Postman for the other)
*/
if ( (request.body.user === "peter" && request.body.password === "terps") ||
(request.body.user === "mary" && request.body.password === "testudo") ) {
request.session.user = request.body.user;
request.session.cart = "";
request.session.save();
message = "User has logged in";
} else {
message = "Invalid user";
}
response.send(message);
});
app.get("/browse", (request, response) => {
let message;
if (request.session.user != undefined) {
message = `Welcome back ${request.session.user}, browse`;
} else {
message = "You have not logged in";
}
response.send(message);
});
app.post("/buy", (request, response) => {
let message;
if (request.session.user != undefined) {
request.session.cart += request.body.item + " ";
message = `${request.body.item} added to your cart`;
} else {
message = "You have not logged in";
}
response.send(message);
});
app.post("/checkout", (request, response) => {
let message;
if (request.session.user != undefined) {
message = `Items you are buying are ${request.session.cart}`;
} else {
message = "You have not logged in";
}
response.send(message);
});
app.post("/logout", (request, response) => {
let message;
if (request.session.user != undefined) {
request.session.destroy();
message = "You have logged out";
} else {
message = "You were not logged in";
}
response.send(message);
});
console.log(`Server listening on port ${portNumber}`);
app.listen(portNumber);
{
"name": "cookies",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"cookie-parser": "^1.4.6",
"express": "^4.18.1",
"express-session": "^1.17.2",
"nodemon": "^2.0.16"
}
}
{
"name": "RouterCookiesSessionsCode",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment