-
Notifications
You must be signed in to change notification settings - Fork 218
Develop #164
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: master
Are you sure you want to change the base?
Develop #164
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,81 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const DATA_PATH = path.resolve('db', 'expense.json'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const indexPath = path.resolve('src', 'index.html'); | ||
|
|
||
|
|
||
| if (req.method === 'GET' && req.url === '/') { | ||
| try { | ||
| const file = fs.readFileSync(indexPath); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(file); | ||
| } catch { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('file not found'); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
|
|
||
| if (req.method === 'POST' && req.url === '/add-expense') { | ||
|
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. There's a mismatch between the endpoint you're listening to here ( For the form submission to work, these two URLs must be identical. |
||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| req.on('end', () => { | ||
| try { | ||
| const buffer = Buffer.concat(chunks).toString(); | ||
| const obj = JSON.parse(buffer); | ||
|
|
||
| if (!obj.date || !obj.title || !obj.amount) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid expense data'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
|
|
||
| fs.writeFileSync(DATA_PATH, JSON.stringify(obj, null, 2)); | ||
|
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. This is the same issue from the previous review. This line overwrites To fix this, you need to:
|
||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(obj)); | ||
|
Comment on lines
+49
to
+50
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. The task requires returning an HTML page that displays the well-formatted JSON from the Currently, your code returns a raw JSON response (
Comment on lines
+49
to
+50
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. The task requires you to "return an HTML page with well formatted JSON". Currently, you're sending back raw JSON with a To meet the requirements, you should:
Comment on lines
+49
to
+50
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. This response does not meet the task requirements, which was also mentioned in the previous review. The task asks you to "return an HTML page with well formatted JSON". Currently, you are returning raw JSON data ( Instead, you should:
|
||
| } catch { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid JSON'); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
| if (req.method === 'GET' && req.url === '/expenses') { | ||
| try { | ||
| const data = fs.existsSync(DATA_PATH) | ||
| ? fs.readFileSync(DATA_PATH, 'utf-8') | ||
| : ''; | ||
| const obj = data ? JSON.parse(data) : {}; | ||
|
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. When the |
||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(obj)); | ||
| } catch { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('Failed to read expense'); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not found'); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| }; | ||
| module.exports = { createServer }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Add Expense</title> | ||
| </head> | ||
| <body> | ||
| <h1>Add Expense</h1> | ||
| <form id="expense-form"> | ||
| <label>Title: <input type="text" name="title" required></label><br> | ||
| <label>Date: <input type="date" name="date" required></label><br> | ||
| <label>Amount: <input type="number" name="amount" required></label><br> | ||
| <button type="submit">Add Expense</button> | ||
| </form> | ||
|
|
||
| <h2>All Expenses</h2> | ||
| <div id="expenses"></div> | ||
|
|
||
| <script> | ||
| const form = document.getElementById('expense-form'); | ||
| const expensesDiv = document.getElementById('expenses'); | ||
|
|
||
| async function loadExpenses() { | ||
| try { | ||
| const res = await fetch('/db/expense.json'); | ||
|
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. This Based on |
||
| const arr = await res.json(); | ||
| expensesDiv.innerHTML = `<pre>${JSON.stringify(arr, null, 2)}</pre>`; | ||
| } catch { | ||
| expensesDiv.innerHTML = 'No expenses yet.'; | ||
| } | ||
| } | ||
|
|
||
| form.addEventListener('submit', async e => { | ||
| e.preventDefault(); | ||
|
|
||
| const data = { | ||
| title: form.title.value, | ||
| date: form.date.value, | ||
| amount: form.amount.value | ||
| }; | ||
|
|
||
| await fetch('/submit-expense', { | ||
|
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. The URL for the POST request here is To fix this, make sure the URL in this |
||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(data) | ||
| }); | ||
|
|
||
| form.reset(); | ||
| loadExpenses(); | ||
| }); | ||
|
|
||
| loadExpenses(); | ||
| </script> | ||
| </body> | ||
| </html> | ||
|
|
||
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.
This route handler is set up for the path
/add-expense, but the form insrc/index.htmlis configured to submit data to a different URL (/expense). These paths need to match for the server to process the form submission.