-
Notifications
You must be signed in to change notification settings - Fork 240
Solution #195
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 #195
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,107 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
|
|
||
| const form = ` | ||
| <form method="POST" action="/add-expense"> | ||
| <input | ||
| type="date" | ||
| name="date" | ||
| required | ||
| > | ||
| <input | ||
| type="text" | ||
| name="title" | ||
| placeholder="Enter title" | ||
| required | ||
| > | ||
| <input | ||
| type="number" | ||
| name="amount" | ||
| placeholder="Enter amount" | ||
| required | ||
| > | ||
| <button type="submit">Send</button> | ||
| </form> | ||
| `; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'GET') { | ||
| if (req.url === '/') { | ||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(form); | ||
| } else if (req.url === '/add-expense') { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Incorrect request'); | ||
| } else { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Page not found'); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'POST') { | ||
| if (req.url === '/add-expense') { | ||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => (body += chunk)); | ||
|
|
||
| req.on('end', () => { | ||
| let expenseData; | ||
|
|
||
| if (req.headers['content-type'] === 'application/json') { | ||
| expenseData = JSON.parse(body); | ||
| } else { | ||
| const params = new URLSearchParams(body); | ||
|
|
||
| expenseData = Object.fromEntries(params); | ||
| } | ||
|
|
||
| const dateObj = new Date(expenseData.date); | ||
| const isValidDate = dateObj instanceof Date && !isNaN(dateObj); | ||
|
|
||
| const isValidTitle = | ||
| expenseData.title && expenseData.title.trim().length > 0; | ||
|
|
||
| const amount = parseFloat(expenseData.amount); | ||
| const isValidAmount = !isNaN(amount) && amount > 0; | ||
|
|
||
| if (!isValidDate || !isValidTitle || !isValidAmount) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid data'); | ||
| } else { | ||
| const filePath = 'db/expense.json'; | ||
|
|
||
| if (!fs.existsSync('db')) { | ||
| fs.mkdirSync('db'); | ||
| } | ||
|
|
||
| if (!fs.existsSync(filePath)) { | ||
| fs.writeFileSync(filePath, JSON.stringify({})); | ||
| } | ||
|
|
||
| // const raw = fs.readFileSync(filePath, 'utf8'); | ||
| // const arr = JSON.parse(raw); | ||
| const record = { | ||
| date: expenseData.date, | ||
| title: expenseData.title, | ||
| amount: expenseData.amount, | ||
| }; | ||
|
|
||
| // arr.push(record); | ||
|
|
||
| fs.writeFileSync(filePath, JSON.stringify(record, null, 2)); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(expenseData)); | ||
|
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 requirement is to return an HTML page containing well-formatted JSON. This implementation sends a raw JSON response with the |
||
| } | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| 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.
The current implementation overwrites the
expense.jsonfile with only the latest record. To meet the requirement of saving the expense data, you should append new expenses to the existing list. This typically involves reading the file, parsing the JSON into an array, adding the new record, and then writing the updated array back to the file.Also, consider what the initial content of the file should be on line 83. If you plan to store a list of expenses, an empty array
[]would be more suitable than an empty object{}.