-
Notifications
You must be signed in to change notification settings - Fork 236
added solution #193
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?
added solution #193
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
| 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 |
|---|---|---|
| @@ -1,8 +1,63 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| const fileStream = fs.createReadStream('src/index.html'); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); | ||
| fileStream.pipe(res); | ||
|
|
||
| fileStream.on('error', () => { | ||
| res.statusCode = 500; | ||
|
|
||
| res.end('Server error'); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'POST' && req.url === '/add-expense') { | ||
| let body = ''; | ||
|
|
||
| req.on('data', (chunck) => (body += chunck)); | ||
|
|
||
| req.on('end', () => { | ||
| const data = 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. The body of a standard form submission is in 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 HTML form sends data in the You should use a different method to parse this kind of data. Consider using Node's built-in |
||
|
|
||
| // eslint-disable-next-line | ||
| console.log(body); | ||
| // eslint-disable-next-line | ||
| console.log(data); | ||
|
|
||
| if (!data.date || !data.title || !data.amount) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Missing some fields'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const expense = { | ||
| date: data.date, | ||
| title: data.title, | ||
| amount: data.amount, | ||
| }; | ||
|
|
||
| fs.writeFileSync('db/expense.json', JSON.stringify(expense)); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(expense)); | ||
|
Comment on lines
+51
to
+52
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 returning an 'HTML page with well formatted JSON'. Your implementation returns a raw JSON response. You need to change the
Comment on lines
+51
to
+52
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 returning an HTML page that displays the saved JSON data. Currently, you're sending a raw JSON response with
Comment on lines
+51
to
+52
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. According to the task description, the server must return an HTML page that displays the saved data, not a raw JSON response. You'll need to change the |
||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not Found'); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <!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="application/x-www-form-urlencoded" | ||
| > | ||
| <div> | ||
| <p>Date</p> | ||
| <input type="date" name="date" /> | ||
| </div> | ||
| <div> | ||
| <p>Title</p> | ||
| <input type="text" name="title" /> | ||
| </div> | ||
| <div> | ||
| <p>Amount</p> | ||
| <input type="number" name="amount" step="0.01" /> | ||
| </div> | ||
|
|
||
| <div style="margin-top: 5px"> | ||
| <button type="submit">Submit</button> | ||
| </div> | ||
| </form> | ||
|
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 request body from an HTML form with You need to parse this specific format. Consider using Node.js's built-in |
||
| </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.
HTML forms submitted with the default
enctypesend data asapplication/x-www-form-urlencoded, not as a JSON string. UsingJSON.parse()on this data will result in an error. You should use a tool like Node's built-inquerystringmodule or theURLSearchParamsclass to parse the form data correctly.