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

Oleksandr Week 1 Node JS #7

Open
wants to merge 4 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "node",
"version": "1.0.0",
"description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.21.0",
"express-handlebars": "^8.0.1",
"node-fetch": "^3.3.2",
"nodemon": "^3.1.7"
}
}
16 changes: 16 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from "express";

const app = express();

app.use(express.json());

app.get("/", (req, res) => {
res.send("hello from backend to frontend!");
});

app.post("/weather", (req, res) => {
const cityName = req.body.cityName;
res.send({ cityName });
});

app.listen(3000);
5 changes: 2 additions & 3 deletions week1/practice-exercises/1-pad-numbers/padLeft.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

/**
* Inserts a certain character until a has the desired length
* e.g. padLeft('foo', 5, '_') -> '__foo'
* e.g. padLeft( '2', 2, '0') -> '02'
*/
function padLeft(val, num, str) {
return '00000'.replace(/0/g, str).slice(0, num - val.length) + val;
return "00000".replace(/0/g, str).slice(0, num - val.length) + val;
}

// YOUR CODE GOES HERE
export { padLeft };
17 changes: 10 additions & 7 deletions week1/practice-exercises/1-pad-numbers/script.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@

import { padLeft } from "./padLeft.js";
/**
** Exercise 1: Pad numbers
*
** Exercise 1: Pad numbers
*
* In this file use the padLeft function from padLeft.js to
* pad the numbers to exactly 5 spaces and log them to the console
*
*
* Expected output (replace the underscore with spaces):
*
*
* ___12;
* __846;
* ____2;
* _1236;
*
*
* Tips:
* where to use `exports` and where `require`?
*/

let numbers = [ "12", "846", "2", "1236" ];
let numbers = ["12", "846", "2", "1236"];

// YOUR CODE GOES HERE
numbers.forEach((value) => {
console.log(padLeft(value, 5, " "));
});
15 changes: 15 additions & 0 deletions week1/practice-exercises/2-left-pad/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "2-left-pad",
"version": "1.0.0",
"description": "A senior developer from your team Slacks you that he tried to pad some numbers to 8 characters and it was not working at all. He asks you (politely) to fix the bug as soon as possible or face the wrath of management.",
"main": "padLeft.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"left-pad": "^1.3.0"
}
}
12 changes: 8 additions & 4 deletions week1/practice-exercises/2-left-pad/script.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const leftPad = require("left-pad");

/**
** Exercise 2: To the left, to the left...
*
** Exercise 2: To th xxe left, to the left...
*
* Copy and paste your code from the previous exercise.
* Replace the function `padLeft` to use
* this new NPM package called `left-pad` instead then
* Pad the numbers to 8 characters to confirm that it works correctly
*
*/

let numbers = [ "12", "846", "2", "1236" ];
let numbers = ["12", "846", "2", "1236"];

// YOUR CODE GOES HERE
numbers.forEach((value) => {
console.log(leftPad(value, 8));
});
20 changes: 12 additions & 8 deletions week1/prep-exercises/1-web-server/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<html>
<head>
<title>My First Web Server</title>
</head>
<body>
<h1>Hello, anyone there?</h1>
<div id="content"></div>
<script src="index.js"></script>
</body>

<head>
<title>My First Web Server</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
<h1>Hello, anyone there?</h1>
<div id="content"></div>
<script src="index.js"></script>
</body>

</html>
3 changes: 2 additions & 1 deletion week1/prep-exercises/1-web-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "Andrej Gajduk",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
"express": "^4.17.1",
"nodemon": "^3.1.7"
}
}
32 changes: 22 additions & 10 deletions week1/prep-exercises/1-web-server/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
/**
* Exercise 3: Create an HTTP web server
*/
const http = require("http");
const fs = require("fs");

const http = require('http');

//create a server
let server = http.createServer(function (req, res) {
// YOUR CODE GOES IN HERE
res.write('Hello World!'); // Sends a response back to the client
res.end(); // Ends the response
if (req.url === "/") {
readData("index.html", "text/html", res);
} else if (req.url === "/index.js") {
readData("index.js", "application/javascript", res);
} else if (req.url === "/style.css") {
readData("style.css", "text/css", res);
}
});

server.listen(3000); // The server starts to listen on port 3000
const readData = (file, type, res) => {
fs.readFile(file, (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end(err.message);
}

res.writeHead(200, { "Content-Type": type });
res.end(data);
});
};

server.listen(3000);
3 changes: 3 additions & 0 deletions week1/prep-exercises/1-web-server/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: rgb(238, 238, 238);
}