-
Notifications
You must be signed in to change notification settings - Fork 218
Solution #173
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 #173
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,8 +1,78 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
|
|
||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| if (req.method === 'POST' && req.url === '/add-expense') { | ||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body += chunk.toString(); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| let fields; | ||
|
|
||
| try { | ||
| fields = JSON.parse(body); | ||
|
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 parsing method isn't correct for the data sent by the HTML form. Forms using You'll need to parse this string differently. Consider using Node's built-in |
||
| } catch (err) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Invalid JSON'); | ||
| } | ||
|
|
||
| if (!fields.amount || !fields.date || !fields.title) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing required fields'); | ||
| } | ||
|
|
||
| const filePath = 'db/expense.json'; | ||
|
|
||
| const newData = { | ||
| amount: fields.amount, | ||
| date: fields.date, | ||
| title: fields.title, | ||
| }; | ||
|
|
||
| fs.writeFileSync(filePath, JSON.stringify(newData)); | ||
|
|
||
| const file = fs.readFileSync(filePath); | ||
|
|
||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(file); | ||
|
Comment on lines
+50
to
+51
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 response for a successful submission needs two changes to meet the requirements:
|
||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.url !== '/' && req.url !== '/add-expense') { | ||
|
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 condition correctly routes |
||
| res.statusCode = 404; | ||
|
|
||
| return res.end('Invalid Url'); | ||
| } | ||
|
|
||
| res.setHeader('Content-type', 'text/html'); | ||
|
|
||
| res.end( | ||
| `<form method="POST" action="/add-expense"> | ||
| <input name="date" type="date"> | ||
| <input name="title" type="text"> | ||
| <input name="amount" type="number"> | ||
| <button type="submit">Submit</button> | ||
| </form>`, | ||
| ); | ||
| }); | ||
|
|
||
| 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 request handler is missing a crucial part of the requirements. The application must also 'show an HTML form'. You should add logic here to handle
GETrequests to serve this form to the user.