-
Notifications
You must be signed in to change notification settings - Fork 240
Develop #157
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 #157
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,89 @@ | ||
| 'use strict'; | ||
|
|
||
| const { Server } = require('node:http'); | ||
| const path = require('node:path'); | ||
| const { pipeline } = require('node:stream'); | ||
| const { createReadStream, createWriteStream } = require('node:fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new Server(); | ||
|
|
||
| server.on('request', async (req, res) => { | ||
| const { pathname } = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (pathname === '/' || pathname === '/index.html') { | ||
| const indexPath = path.resolve(__dirname, 'index.html'); | ||
| const readStream = createReadStream(indexPath); | ||
|
|
||
| pipeline(readStream, res, (err) => { | ||
| if (err) { | ||
| sendResponse(res, 500, 'Error reading index.html'); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (pathname === '/add-expense') { | ||
| if (req.method === 'GET') { | ||
| return sendResponse( | ||
| res, | ||
| 400, | ||
| 'GET requests are not allowed on /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 error message here says 'GET requests are not allowed on /submit-expense', but the correct endpoint is '/add-expense'. This could confuse users and should be updated to match the actual route. 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 error message references |
||
| ); | ||
| } | ||
|
|
||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body += chunk.toString(); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| let fields; | ||
|
|
||
| try { | ||
| fields = JSON.parse(body); | ||
| } catch (err) { | ||
| return sendResponse(res, 400, 'Invalid JSON'); | ||
| } | ||
|
|
||
| const { date, title, amount } = fields; | ||
|
|
||
| if (!date || !title || !amount) { | ||
| return sendResponse(res, 400, 'Missing required fields'); | ||
| } | ||
|
|
||
| const jsonPath = path.resolve(__dirname, '..', 'db', 'expense.json'); | ||
|
|
||
| try { | ||
| const writeStream = createWriteStream(jsonPath); | ||
|
|
||
| writeStream.on('error', () => { | ||
| return sendResponse(res, 500, 'Error saving expense data'); | ||
| }); | ||
|
|
||
| writeStream.write(JSON.stringify(fields), () => { | ||
| writeStream.end(); | ||
| res.writeHead(200, { 'content-type': 'application/json' }); | ||
| res.end(JSON.stringify(fields)); | ||
| }); | ||
|
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 logic will overwrite the entire 'expense.json' file with each new expense, rather than appending or merging new expense data. If the checklist requires storing multiple expenses, you should read the existing file, parse it as an array, add the new expense, and then write the updated array back to the file. 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. Critical issue: This logic overwrites the entire 'expense.json' file with only the most recent expense. According to the requirements, you should maintain a list of expenses (i.e., append the new expense to an array in the JSON file, not replace it). Please update this section to read the existing expenses, add the new one, and then write the updated array back to the file. |
||
| } catch (err) { | ||
| sendResponse(res, 500, 'Error saving expense'); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
| sendResponse(res, 404, 'Not Found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| function sendResponse(res, statusCode, message) { | ||
| res.writeHead(statusCode, { 'content-type': 'text/plain' }); | ||
| res.end(message); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Form data</title> | ||
| </head> | ||
| <body> | ||
| <h1>Form Data</h1> | ||
| <form action="/submit-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. The form's action is set to '/submit-expense', but your server expects POST requests at '/add-expense'. Update the action to '/add-expense' to match your server route. |
||
| <label for="data">Data:</label> | ||
| <input type="data" id="data" name="data"><br> | ||
|
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 input type 'data' is not valid HTML. You likely meant 'date' for a date picker. Please change type="data" to type="date". |
||
|
|
||
| <label for="title">Title:</label> | ||
| <input type="text" id="title" name="title"><br> | ||
|
|
||
| <label for="amount">Amount:</label> | ||
| <input type="number" id="amount" name="amount"><br> | ||
|
|
||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </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.
The error message refers to '/submit-expense', but the route being handled is '/add-expense'. This may be confusing for users and is likely a mistake. Please update the error message to refer to '/add-expense'.