-
Notifications
You must be signed in to change notification settings - Fork 240
Solution #148
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?
Solution #148
Changes from 2 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,8 +1,70 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const http = require('http'); | ||
|
|
||
| const requestHandler = (req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| const filePath = path.join(__dirname, 'index.html'); | ||
|
|
||
| fs.readFile(filePath, (err, data) => { | ||
| if (err) { | ||
| res.statusCode = 500; | ||
| res.end('Internal Server Error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.setHeader('Content-Type', 'text/html'); | ||
| res.end(data); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'POST' && req.url === '/add-expense') { | ||
| const filePath = path.join(__dirname, '../db/expense.json'); | ||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body += chunk; | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| let expense; | ||
|
|
||
| try { | ||
| expense = JSON.parse(body); | ||
| } catch { | ||
| res.statusCode = 400; | ||
| res.end('Invalid JSON'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!expense.date || !expense.title || !expense.amount) { | ||
| res.statusCode = 400; | ||
| res.end('Missing required fields'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFileSync(filePath, JSON.stringify(expense, 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. Issue: This line overwrites the entire 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. Issue: The current logic overwrites the entire |
||
|
|
||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(expense)); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Not found'); | ||
| }; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer(requestHandler); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <form action="/add-expense" method="POST"> | ||
|
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. Issue: The form uses the default submission method, which sends data as 'application/x-www-form-urlencoded'. Your server expects JSON in the POST body, so this will not work. You need to either update the server to handle form-encoded data or use JavaScript to intercept the form submission and send the data as JSON using fetch or XMLHttpRequest. 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. Issue: The form submits data as
Without this change, expense submissions from this form will not be processed correctly by your server. |
||
| <label>Date: <input type="date" name="date" required /></label><br/> | ||
| <label>Title: <input type="text" name="title" required /></label><br/> | ||
| <label>Amount: <input type="number" name="amount" required /></label><br/>А | ||
| <button type="submit">Submit</button> | ||
| </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.
Issue: The current implementation overwrites the entire 'expense.json' file with each new expense. This means only the most recent expense will be saved. If the intention is to keep a list of expenses, you should read the existing file, parse it as an array, push the new expense, and then write the updated array back to the file.