diff --git a/homework/hackyourtemperature/app.js b/homework/hackyourtemperature/app.js
new file mode 100644
index 000000000..381644c1c
--- /dev/null
+++ b/homework/hackyourtemperature/app.js
@@ -0,0 +1,37 @@
+import fetch from 'node-fetch';
+import express from 'express';
+import bodyParser from 'body-parser';
+import keys from "./sources/keys.js";
+
+const app = express();
+
+
+app.use(bodyParser.json());
+
+app.get('/', (req, res) => {
+ res.send("
Hello from backend to frontend!
");
+ res.end();
+})
+
+app.post('/weather' , async(req , res) => {
+ try{
+ const city = req.body.cityName;
+ if(!city){
+ throw new Error("city name is missing");
+ }
+
+ const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&APPID=${keys.API_KEY}`);
+ const data = await response.json();
+ if(!data){
+ res.send({ weatherText: "City is not found!" });
+ }
+ const temperature = data.main.temp;
+ res.send({ weatherText: `The temperature in ${city} is:${temperature}!.` });
+ }catch(error) {
+ res.status(400).json({error : error.message});
+ };
+});
+
+
+
+export default app;
diff --git a/homework/hackyourtemperature/babel.config.cjs b/homework/hackyourtemperature/babel.config.cjs
new file mode 100644
index 000000000..fbb629af6
--- /dev/null
+++ b/homework/hackyourtemperature/babel.config.cjs
@@ -0,0 +1,13 @@
+module.exports = {
+ presets: [
+ [
+ // This is a configuration, here we are telling babel what configuration to use
+ "@babel/preset-env",
+ {
+ targets: {
+ node: "current",
+ },
+ },
+ ],
+ ],
+};
diff --git a/homework/hackyourtemperature/jest.config.js b/homework/hackyourtemperature/jest.config.js
new file mode 100644
index 000000000..19ba9649e
--- /dev/null
+++ b/homework/hackyourtemperature/jest.config.js
@@ -0,0 +1,8 @@
+export default {
+ // Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer
+ transform: {
+ "^.+\\.jsx?$": "babel-jest",
+ },
+ // By default our `node_modules` folder is ignored by jest, this tells jest to transform those as well
+ transformIgnorePatterns: [],
+};
diff --git a/homework/hackyourtemperature/package.json b/homework/hackyourtemperature/package.json
new file mode 100644
index 000000000..de9d2fb82
--- /dev/null
+++ b/homework/hackyourtemperature/package.json
@@ -0,0 +1,26 @@
+{
+ "name": "hackyourtemperature",
+ "version": "1.0.0",
+ "description": "",
+ "main": "server.js",
+ "type": "module",
+ "scripts": {
+ "test": "jest",
+ "start": "node server.js"
+ },
+ "keywords": [],
+ "author": "",
+ "license": "ISC",
+ "dependencies": {
+ "body-parser": "^1.20.2",
+ "express": "^4.18.2",
+ "node-fetch": "^3.3.2",
+ "nodemon": "^3.0.1"
+ },
+ "devDependencies": {
+ "@babel/preset-env": "^7.22.10",
+ "babel-jest": "^29.6.2",
+ "jest": "^29.6.2",
+ "supertest": "^6.3.3"
+ }
+}
diff --git a/homework/hackyourtemperature/server.js b/homework/hackyourtemperature/server.js
new file mode 100644
index 000000000..fbd52a416
--- /dev/null
+++ b/homework/hackyourtemperature/server.js
@@ -0,0 +1,7 @@
+import app from "./app.js";
+
+const port = 3000;
+
+app.listen(port, ()=> {
+ console.log(`Server is running on port ${port}`);
+});
\ No newline at end of file
diff --git a/homework/hackyourtemperature/sources/keys.js b/homework/hackyourtemperature/sources/keys.js
new file mode 100644
index 000000000..f80cfab22
--- /dev/null
+++ b/homework/hackyourtemperature/sources/keys.js
@@ -0,0 +1,5 @@
+const keys = {
+ API_KEY : "1af1273248108446771541da5911dda3"
+}
+
+export default keys;
\ No newline at end of file
diff --git a/homework/hackyourtemperature/tests/app.test.js b/homework/hackyourtemperature/tests/app.test.js
new file mode 100644
index 000000000..f827d6fc1
--- /dev/null
+++ b/homework/hackyourtemperature/tests/app.test.js
@@ -0,0 +1,72 @@
+import app from "../app.js";
+import supertest from "supertest";
+
+const request = supertest(app);
+
+//Testing http get request
+describe("GET/", ()=> {
+it("Should send back a message 'Hello from backend to frontend'", (done)=> {
+ request.get('/')
+ .send('Hello from backend to frontend!')
+ .end(function(err, res) {
+ if (err) return done(err);
+ done();
+ });
+});
+
+it("Should send a status 200", (done)=> {
+ request.get('/')
+ .expect(200)
+ .end(function(err, res) {
+ if (err) return done(err);
+ done();
+ });
+});
+
+});
+
+//Testing http post request
+describe("POST/weather", ()=> {
+ it("Should send a status 200 in case of sending correct city name", (done)=> {
+ request.post('/weather')
+ .send({cityName:"London"})
+ .expect(200)
+ .end(function(err, res) {
+ if (err) return done(err);
+ done();
+ });
+ });
+
+ it("Should send /city name is missing/ in case of not sending a not proper city name", (done)=> {
+ request.post('/weather')
+ .send({})
+ .expect({error : "city name is missing"})
+ .end(function(err, res) {
+ if (err) return done(err);
+ done();
+ });
+ });
+
+ it("Should sent 400 if the city was not found", (done)=> {
+ request.post('/weather')
+ .send({cityName: "Londom"})
+ .expect(400)
+ .end(function(err, res) {
+ if (err) return done(err);
+ done();
+ });
+ });
+
+ it("Should sent a message contains the temperature information if the city was found", (done)=> {
+ request.post('/weather')
+ .send({cityName: "London"})
+ .expect((res) => {
+ expect(res.body.weatherText).toContain("The temperature in London is")
+ })
+ .end(function(err, res) {
+ if (err) return done(err);
+ done();
+ });
+ });
+
+ });
diff --git a/week2/prep-exercises/1-blog-API/my first blog b/week2/prep-exercises/1-blog-API/my first blog
new file mode 100644
index 000000000..afef39d4b
--- /dev/null
+++ b/week2/prep-exercises/1-blog-API/my first blog
@@ -0,0 +1 @@
+the first blog is very beautiful
\ No newline at end of file
diff --git a/week2/prep-exercises/1-blog-API/package.json b/week2/prep-exercises/1-blog-API/package.json
index d89c4bd76..b8c797865 100644
--- a/week2/prep-exercises/1-blog-API/package.json
+++ b/week2/prep-exercises/1-blog-API/package.json
@@ -4,12 +4,17 @@
"description": "",
"main": "server.js",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1",
+ "test": "jest",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
- "express": "^4.17.1"
+ "express": "^4.17.1",
+ "jest": "^29.6.2",
+ "nodemon": "^3.0.1"
+ },
+ "devDependencies": {
+ "supertest": "^6.3.3"
}
}
diff --git a/week2/prep-exercises/1-blog-API/server.js b/week2/prep-exercises/1-blog-API/server.js
index 3f615e8f5..430861ab0 100644
--- a/week2/prep-exercises/1-blog-API/server.js
+++ b/week2/prep-exercises/1-blog-API/server.js
@@ -1,10 +1,86 @@
const express = require('express')
+const fs = require("fs");
const app = express();
-// YOUR CODE GOES IN HERE
+app.use(express.json());
+
+//create a new post
+app.post('/blogs', (req, res) => {
+ const {title, content} = req.body;
+ if(!title || !content) {
+ res.status(400).json({Error: "Either title or content are missing"});
+ }
+ fs.writeFileSync(title, content);
+ res.end('ok')
+})
+
+// update a post
+app.put('/blogs/:title', (req, res) => {
+ const title = req.params.title;
+ const content = req.body.content;
+ if(!content) {
+ res.status(400).json({Error: " there is no content"});
+ return;
+ }
+ if (fs.existsSync(title)) {
+ fs.writeFileSync(title, content);
+ res.end('ok')
+ }
+ else {
+
+ res.status(400).json({Error: "post was not found"});
+ }
+})
+
+
+//delete a post
+
+app.delete('/blogs/:title', (req, res) => {
+ const title = req.params.title;
+ if (fs.existsSync(title)) {
+ fs.unlinkSync(title);
+ res.end('ok');
+ } else {
+ res.status(400).json({Error: "the post doesn't exist"});
+ }
+})
+
+
+// read posts
+
+app.get('/blogs/:title', (req, res) => {
+
+ const title = req.params.title;
+ const content = req.body.content;
+ if(fs.existsSync(title)){
+ const post = fs.readFileSync(title, 'utf-8');
+
+ const response = {
+ title: title,
+ content: post
+ }
+ res.status(200).json(response);
+ }
+ else{
+ res.status(400).end("The post was not found");
+ }
+})
+
+app.get("/blogs", (req, res) => {
+ const files = [];
+ const fileNames = fs.readdirSync("/");
+ fileNames.forEach((fileName) => {
+ files.push({title: fileName});
+ })
+ res.status(200);
+ res.send(files);
+})
+
app.get('/', function (req, res) {
res.send('Hello World')
})
-app.listen(3000)
\ No newline at end of file
+app.listen(3000)
+
+module.exports = app;
\ No newline at end of file
diff --git a/week2/prep-exercises/1-blog-API/server.spec.js b/week2/prep-exercises/1-blog-API/server.spec.js
new file mode 100644
index 000000000..f4cbd8fd1
--- /dev/null
+++ b/week2/prep-exercises/1-blog-API/server.spec.js
@@ -0,0 +1,17 @@
+import { describe } from "node:test";
+
+const request = require('supertest');
+const assert = require('assert');
+const express = require('express');
+const app = require("./server");
+
+describe('')
+request(app)
+ .get('/blogs')
+ .expect('Content-Type', /json/)
+ //.expect('Content-Length', '15')
+ .expect(200)
+ .end(function(err, res) {
+ if (err) throw err;
+ });
+