diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 4fec15ae8..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "editor.tabSize": 2, - "javascript.validate.enable": false, - "editor.codeActionsOnSave": { - "source.fixAll.eslint": "explicit" - } -} diff --git a/assignments/hackyourtemperature/.gitignore b/assignments/hackyourtemperature/.gitignore new file mode 100644 index 000000000..1dcef2d9f --- /dev/null +++ b/assignments/hackyourtemperature/.gitignore @@ -0,0 +1,2 @@ +node_modules +.env \ No newline at end of file diff --git a/assignments/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json new file mode 100644 index 000000000..e24b66874 --- /dev/null +++ b/assignments/hackyourtemperature/package.json @@ -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" + } +} diff --git a/assignments/hackyourtemperature/server.js b/assignments/hackyourtemperature/server.js new file mode 100644 index 000000000..ac429c6ca --- /dev/null +++ b/assignments/hackyourtemperature/server.js @@ -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; + if(!cityName) { + return res.status(404).json({message: "City name is required."}) + } + res.status(200).json({cityName: cityName}); +}) + +app.listen(PORT); diff --git a/week2/prep-exercises/1-blog-API/package.json b/week2/prep-exercises/1-blog-API/package.json index d89c4bd76..ba6b539d7 100644 --- a/week2/prep-exercises/1-blog-API/package.json +++ b/week2/prep-exercises/1-blog-API/package.json @@ -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", diff --git a/week2/prep-exercises/1-blog-API/server.js b/week2/prep-exercises/1-blog-API/server.js index 3f615e8f5..e67e42eaf 100644 --- a/week2/prep-exercises/1-blog-API/server.js +++ b/week2/prep-exercises/1-blog-API/server.js @@ -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) \ No newline at end of file + +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);