-
Notifications
You must be signed in to change notification settings - Fork 236
form data #194
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?
form data #194
Changes from all 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,5 +1 @@ | ||
| { | ||
| "date": "2024-01-25", | ||
| "title": "Test Expense", | ||
| "amount": "100" | ||
| } | ||
| {"date":"2024-01-25","title":"Test Expense","amount":"100"} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!DOCTYPE html> | ||
| <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> | ||
| <form method="post" action="/submit-expense"> | ||
| <input name="date" type="date"> | ||
| <input placeholder="Title" name="title" type="text"> | ||
| <input placeholder="Costs" name="amount" type="number"> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,75 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('node:http'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| const baseUrl = req.url; | ||
| const dbPath = path.resolve('db', 'expense.json'); | ||
| const mainPath = path.resolve('public', 'index.html'); | ||
|
|
||
| if (baseUrl === '/') { | ||
| const stream = fs.createReadStream(mainPath); | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/html'); | ||
|
|
||
| return res.pipe(stream); | ||
| } | ||
|
|
||
| if (baseUrl === '/add-expense') { | ||
| if (req.method !== 'POST') { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end(); | ||
| } | ||
|
|
||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| try { | ||
| const buffer = Buffer.concat(chunks).toString(); | ||
| const data = JSON.parse(buffer); | ||
|
|
||
| if (!data.date || !data.title || !data.amount) { | ||
| fs.writeFileSync(dbPath, JSON.stringify({})); | ||
|
|
||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
|
|
||
| return res.end(JSON.stringify('Missing fields')); | ||
| } | ||
|
|
||
| fs.writeFileSync(dbPath, JSON.stringify(data)); | ||
|
|
||
| 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. The task requires the server to "return an HTML page with well formatted JSON". To do that, the |
||
|
|
||
| return res.end(JSON.stringify(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. This sends back a raw JSON response. Per the requirements, you should send back an HTML document that contains the formatted JSON data. For example, you could wrap the JSON string in |
||
| } catch (e) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
|
|
||
| return res.end(JSON.stringify({ error: 'Invalid JSON' })); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end(); | ||
| }); | ||
|
|
||
| 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.
According to the task description, the application should show an HTML form. The server is currently only handling POST requests to
/add-expense. You need to add a handler for a GET request to serve the form to the user.