-
Notifications
You must be signed in to change notification settings - Fork 240
done #158
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?
done #158
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,58 @@ | ||
| '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) => { | ||
| if (req.url !== '/add-expense') { | ||
| res.setHeader('Content-type', 'text/plain'); | ||
| res.statusCode = 404; | ||
| res.end('Wrong request url'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| const text = Buffer.concat(chunks).toString(); | ||
|
|
||
| const data = JSON.parse(text); | ||
|
|
||
| if (!data['date'] || !data['title'] || !data['amount']) { | ||
| res.statusCode = 400; | ||
| res.end('Not full data'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const writeStream = fs.createWriteStream(path.resolve('db/expense.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. This line creates a write stream that will overwrite 'db/expense.json' with each new request. If the task requires storing multiple expenses, you should append the new expense to the existing data instead of overwriting the file. 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. Issue: Writing the incoming data directly to |
||
|
|
||
| writeStream.end(text); | ||
|
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 the write stream. If an error occurs while writing to the file, the client will not be informed. Consider adding an 'error' event handler for the write stream to handle such cases. 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. Issue: There is no error handling for file write operations. If writing to the file fails, the client will not be notified. You should handle errors and respond with an appropriate status code and message. |
||
|
|
||
| writeStream.on('finish', () => { | ||
| res.setHeader('Content-type', 'application/json'); | ||
| res.statusCode = 200; | ||
| res.end(text); | ||
| }); | ||
| }); | ||
|
|
||
| req.on('error', (error) => { | ||
| res.statusCode = 400; | ||
| res.end(`Request error: ${error}`); | ||
| }); | ||
| }); | ||
| server.on('error', () => {}); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Add Expense</title> | ||
| </head> | ||
| <body> | ||
| <h1>Expense Form</h1> | ||
| <form action="http://localhost:5701/add-expense" method="POST"> | ||
|
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 form uses the default 'application/x-www-form-urlencoded' encoding, but your server expects JSON data in the request body. You need to either update the server to handle URL-encoded data or use JavaScript on the client side to send JSON via fetch/AJAX. 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. Issue: The form submits data as |
||
| <label>Date: <input type="date" name="date" required /></label><br /> | ||
| <label>Title: <input type="text" name="title" required /></label><br /> | ||
| <label>Amount: <input type="number" name="amount" required /></label | ||
| ><br /> | ||
| <button type="submit">Submit</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.
Issue: The server expects the request body to be JSON, but if the client sends data as
application/x-www-form-urlencoded(which is common for HTML forms),JSON.parse(text)will throw an error. You need to handle both content types or ensure the client sends JSON as required by the task.