Skip to content

Assignments week 1 Konjit #6

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

Open
wants to merge 5 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
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

2 changes: 2 additions & 0 deletions assignments/hackyourtemperature/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
19 changes: 19 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"type": "module",
"main": "server.js",
"scripts": {
"start": "nodemon server.js",
"dev": "node --watch server"
},
Comment on lines +6 to +9

Choose a reason for hiding this comment

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

👍🏻

"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.21.2",
"express-handlebars": "^8.0.1",
"node-fetch": "^3.3.2"
Comment on lines +15 to +17

Choose a reason for hiding this comment

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

you've missed nodemon no biggie though.

}
}
21 changes: 21 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from "express";

const PORT = 3000;

const app = express();
app.use(express.json())


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

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

Choose a reason for hiding this comment

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

Suggested change
const cityName = req.body.cityName;
const { cityName } = req.body;

if(!cityName) {
return res.status(404).json({message: "City name is required."})
}
Comment on lines +15 to +17

Choose a reason for hiding this comment

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

💯

res.status(200).json({cityName: cityName});
})

app.listen(PORT);
5 changes: 3 additions & 2 deletions week2/prep-exercises/1-blog-API/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "1-blog-api",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
"start": "node server.js",
"dev": "node --watch server"
},
"author": "",
"license": "ISC",
Expand Down
122 changes: 113 additions & 9 deletions week2/prep-exercises/1-blog-API/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,114 @@
const express = require('express')
import express from "express";
import path from "path";
import fs from "fs";

const app = express();


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

app.listen(3000)

const blogsDir = path.resolve("blogs");

app.use(express.json());

app.get("/", function (req, res) {
res.send("Hello World");
});

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

if (!title || !content) {
return res.status(400).send({ message: "Content is required." });
}
const filePath = path.join(blogsDir, title);
const submitBlog = () => {
fs.writeFileSync(filePath, content);
res.status(200).send({ message: "Blog is created successfully." });
};

handleFileOperation(submitBlog, res, filePath, content);
});

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

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

const filePath = path.join(blogsDir, title);
checkFileExists(filePath, res);

const updateBlog = () => {
fs.writeFileSync(filePath, content);
res.status(201).send({ message: "Blog is updated successfully" });
};

handleFileOperation(updateBlog, res, filePath, content);
});

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

const filePath = path.join(blogsDir, title);

checkFileExists(filePath, res);

const deleteBlog = () => {
fs.unlinkSync(filePath);
res.status(200).send({ message: "Blog is deleted successfully." });
};
handleFileOperation(deleteBlog, res, filePath);
});

// Read a blog
app.get("/blogs/:title", (req, res) => {
const title = req.params.title;
const filePath = path.join(blogsDir, title);

checkFileExists(filePath, res);

const encoding = "utf-8";
const readBlog = () => {
const post = fs.readFileSync(filePath, encoding);
res.setHeader("Content-Type", "text/plain");
res.status(200).send(post);
};

handleFileOperation(readBlog, res, filePath, encoding);
});

// BONUS: Get all blogs
app.get("/blogs/", (req, res) => {
const blogs = fs.readdirSync(blogsDir);
const getBlogs = () => {
if (!blogs) {
res.status(500).send({ message: "No blog is found." });
}
const blogTitles = blogs.map((blog) => ({ title: blog }));
res.status(200).send(blogTitles);
};

handleFileOperation(getBlogs, res, blogs);
});

/** A helper function that checks if a file operation is a success or failure. */

const handleFileOperation = (operation, ...args) => {
try {
return operation(...args);
} catch (err) {
res.status(500).send({ message: `Error occurred while ${msg}` });
}
};

/*Checks if a specified file exists or not. */
const checkFileExists = (filePath, res) => {
if (!fs.existsSync(filePath)) {
return res.status(404).send("This blog does not exist.");
}
};

app.listen(3000);