-
Notifications
You must be signed in to change notification settings - Fork 240
add task solution for node_form-data #155
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?
Changes from 1 commit
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,59 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer(async (req, res) => { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (req.method === 'POST' && url.pathname === '/add-expense') { | ||
| const chunks = []; | ||
|
|
||
| for await (const chunk of req) { | ||
| chunks.push(chunk); | ||
| } | ||
|
|
||
| const formData = Buffer.concat(chunks).toString(); | ||
|
|
||
| let expense; | ||
|
|
||
| try { | ||
| expense = JSON.parse(formData); | ||
| } catch (err) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Invalid JSON format'); | ||
| } | ||
|
|
||
| const { date, title, amount } = expense; | ||
|
|
||
| if (!date || !title || !amount) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing required fields'); | ||
| } | ||
|
|
||
| const filePath = path.join(__dirname, '../db/expense.json'); | ||
|
|
||
| try { | ||
| fs.writeFileSync(filePath, JSON.stringify(expense, null, 2), 'utf8'); | ||
| } catch (err) { | ||
| res.statusCode = 500; | ||
|
|
||
| return res.end('Failed to save data'); | ||
| } | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
|
|
||
| return res.end(JSON.stringify(expense, null, 2)); | ||
|
Comment on lines
+11
to
+68
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 POST |
||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
|
Comment on lines
+86
to
+87
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 server does not serve an HTML form for submitting expenses, which is a core requirement of the task. You should add a handler for GET requests (e.g., to |
||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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 code overwrites the entire 'expense.json' file with the new expense object each time a POST request is made. This means only the latest expense is saved, and all previous data is lost. If the task requires storing multiple expenses, you should read the existing data, append the new expense, and then write the updated array back to the file.