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

Nodejs week2 hackyourtemprature #27

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
28 changes: 28 additions & 0 deletions assignments/hackyourtemperature/__tests__/app.test.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look good in principle. However, I'll only be able to say with certainty once you commit also the contents of the app.js file :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, Michal. I have written the code, but I forgot to double-check that it has not been committed🤔 I have just discovered that I should go to the file command + s and then it would be somehow added the I should add it to git and commit that why the file was empty

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import app from "../app.js";
import supertest from "supertest";

const request = supertest(app);

describe("POST /weather", () => {
it("should return temperature for a valid city", async () => {
const response = await request
.post("/weather")
.send({ cityName: "Amsterdam" });
expect(response.status).toBe(200);
expect(response.body.weatherText).toContain("Amsterdam");
});

it("should return error message for an invalid city", async () => {
const response = await request
.post("/weather")
.send({ cityName: "InvalidCity" });
expect(response.status).toBe(404);
expect(response.body.weatherText).toBe("City is not found!");
});

it("should return error for missing city name", async () => {
const response = await request.post("/weather").send({});
expect(response.status).toBe(400);
expect(response.body.error).toBe("City name is required.");
});
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be missing the code.

Empty file.
27 changes: 27 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hackyourtemperature",
"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": "jest",
"start": "node server.js",
"dev": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.21.0",
"express-handlebars": "^8.0.1",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"nodemon": "^3.1.7",
"jest": "^29.0.0",
"supertest": "^6.3.3",
"babel-jest": "^29.0.0",
"@babel/preset-env": "^7.20.0"
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be missing the code.

Empty file.
4 changes: 4 additions & 0 deletions assignments/hackyourtemperature/sources/keys.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add this file/folder to the project's git ignore list.
The idea behind loading sensitive details such as API keys from a dedicated file rather than hardcoding them directly into the application files is that the details then don't need to be checked into the project's repository.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for letting me know, I added this to the gitignore 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const keys = {
API_KEY: "005326ffeefecc610f41afaea641bfcd",
};
export default keys;
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"express": "^4.21.0"
}
}
100 changes: 91 additions & 9 deletions week2/prep-exercises/1-blog-API/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
const express = require('express')
const express = require("express");
const fs = require("fs");
const app = express();


// YOUR CODE GOES IN HERE
app.get('/', function (req, res) {
res.send('Hello World')
})

app.listen(3000)

app.use(express.json());

app.get("/", (req, res) => {
res.send("You have built you API 🎉");
});

app.post("/blogs", (req, res) => {
const { title, content } = req.body;

if (!title || !content) {
return res.status(400).send("Title and content are required");
}

try {
fs.writeFileSync(`./${title}.txt`, content);
res.status(201).send("Blog created");
} catch (err) {
res.status(500).send("An error occurred while creating the blog");
}
});

app.put("/blogs/:title", (req, res) => {
const { title } = req.params;
const { content } = req.body;

if (!content) {
return res.status(400).send("Content is required");
}

if (fs.existsSync(`./${title}.txt`)) {
try {
fs.writeFileSync(`./${title}.txt`, content);
res.send("Blog updated");
} catch (err) {
res.status(500).send("An error occurred while updating the blog");
}
} else {
res.status(404).send("This post does not exist!");
}
});

app.delete("/blogs/:title", (req, res) => {
const { title } = req.params;

if (fs.existsSync(`./${title}.txt`)) {
try {
fs.unlinkSync(`./${title}.txt`);
res.send("Blog deleted");
} catch (err) {
res.status(500).send("An error occurred while deleting the blog");
}
} else {
res.status(404).send("This post does not exist!");
}
});

app.get("/blogs/:title", (req, res) => {
const { title } = req.params;

if (fs.existsSync(`./${title}.txt`)) {
try {
const content = fs.readFileSync(`./${title}.txt`, "utf8");
res.send(content);
} catch (err) {
res.status(500).send("An error occurred while reading the blog");
}
} else {
res.status(404).send("This post does not exist!");
}
});

app.get("/blogs", (req, res) => {
try {
const files = fs.readdirSync("./");
const blogs = files
.filter((file) => file.endsWith(".txt"))
.map((file) => ({
title: file.replace(".txt", ""),
}));
res.json(blogs);
} catch (err) {
res.status(500).send("An error occurred while reading the blogs");
}
});

app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});