-
Notifications
You must be signed in to change notification settings - Fork 240
solution #188
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 #188
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,67 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const http = require('http'); | ||
| const path = require('path'); | ||
| const formidable = require('formidable'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| if (req.url === '/' && req.method === 'GET') { | ||
| const filePath = path.resolve(__dirname, 'index.html'); | ||
| const stream = fs.createReadStream(filePath); | ||
|
|
||
| stream.on('error', () => { | ||
| res.statusCode = 500; | ||
|
|
||
| return res.end('Cannot read index.html'); | ||
| }); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
|
|
||
| return stream.pipe(res); | ||
| } | ||
|
|
||
| if (req.method === 'POST' && req.url.startsWith('/add-expense')) { | ||
| const form = new formidable.IncomingForm({ multiples: false }); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.statusCode = 500; | ||
|
|
||
| return res.end('Error parsing the form'); | ||
| } | ||
|
|
||
| const { date, title, amount } = fields || {}; | ||
|
|
||
| if (!date || !title || !amount) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing fields'); | ||
| } | ||
|
|
||
| const filePath = path.resolve(__dirname, '../db/expense.json'); | ||
| let expenses = {}; | ||
|
|
||
| expenses = { date, title, amount }; | ||
|
|
||
| fs.writeFileSync(filePath, JSON.stringify(expenses, null, 2)); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/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. You're sending an HTML string in the response body, which is correct according to the requirements. However, this |
||
|
|
||
| return res.end(JSON.stringify(expenses)); | ||
|
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 asks to 'return an HTML page with well formatted JSON'. This implementation returns a raw JSON response ( |
||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <!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" | ||
| enctype="multipart/form-data" | ||
| > | ||
|
Comment on lines
+9
to
+12
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 form is set up correctly to send data. However, the backend logic in
Please adjust the server-side code to fix these issues. |
||
| <input type="date" name="date" /> | ||
| <input type="text" name="title"/> | ||
| <input type="number" name="amount" id=""> | ||
| <button type="submit">Envoyer</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.
This logic overwrites
db/expense.jsonon every submission. To save a history of expenses, you should read the current contents of the file, add the new expense to the list, and then write the entire list back. Remember to handle cases where the file might not exist or be empty initially.