-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: main
Are you sure you want to change the base?
Changes from all commits
761ca1e
fbad619
ab7f0a7
499b569
dfb7fe7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
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" | ||
}, | ||
"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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you've missed |
||
} | ||
} |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if(!cityName) { | ||||||
return res.status(404).json({message: "City name is required."}) | ||||||
} | ||||||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||||||
res.status(200).json({cityName: cityName}); | ||||||
}) | ||||||
|
||||||
app.listen(PORT); |
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻