-
Notifications
You must be signed in to change notification settings - Fork 240
Implement node form data solution #161
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 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Form data</title> | ||
| </head> | ||
| <body> | ||
| <h1>Form data for expense</h1> | ||
| <form action="/add-expense" method="POST"> | ||
| <label for="date">Date:</label><br> | ||
| <input | ||
| type="date" | ||
| name="date" | ||
| id="date" | ||
| required | ||
| /> | ||
|
|
||
| <br><br> | ||
|
|
||
| <label for="title">Title:</label><br> | ||
| <input | ||
| id="title" | ||
| name="title" | ||
| type="text" | ||
| required | ||
| /> | ||
|
|
||
| <br><br> | ||
|
|
||
| <label for="amount">Amount:</label><br> | ||
| <input | ||
| type="number" | ||
| name="amount" | ||
| id="amount" | ||
| required | ||
| /> | ||
|
|
||
| <br><br> | ||
|
|
||
| <button type="submit">Send request</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,55 @@ | ||
| 'use strict'; | ||
| '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 | ||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (url.pathname === '/' && req.method === 'GET') { | ||
| fs.createReadStream(path.resolve('public', 'index.html')).pipe(res); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (url.pathname === '/add-expense' && req.method === 'POST') { | ||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| const expensePath = path.resolve(__dirname, '..', 'db/expense.json'); | ||
| const data = Buffer.concat(chunks).toString(); | ||
|
|
||
| if (Object.keys(JSON.parse(data)).length !== 3) { | ||
|
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 code only checks that there are 3 keys in the parsed data, but does not check that the required fields (date, title, amount) are present and valid. The requirements specify that missing or invalid form data should be handled gracefully. |
||
| res.statusCode = 400; | ||
| res.setHeader('Content-type', 'text/plain'); | ||
| res.end('All params must be completed'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFileSync(expensePath, data); | ||
|
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. There is no check to ensure the 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. There is no error handling for file operations (e.g., if writing fails). The requirements specify that file errors must be handled gracefully, with an appropriate response to the client. |
||
| res.statusCode = 200; | ||
| res.setHeader('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. After saving, the response is sent as raw JSON with content type |
||
| res.end(data); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Page not found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| 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 POST handler assumes the request body is JSON. However, standard HTML forms submit data as
application/x-www-form-urlencoded. You need to parse the body accordingly, or ensure the form sends JSON. This is a critical requirement for correct form handling.