-
Notifications
You must be signed in to change notification settings - Fork 240
Solution #144
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 #144
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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| module.exports = { | ||
| root: true, | ||
| extends: '@mate-academy/eslint-config', | ||
| env: { | ||
| jest: true | ||
| jest: true, | ||
| }, | ||
| plugins: ['jest'], | ||
| rules: { | ||
| 'no-proto': 0 | ||
| 'no-proto': 0, | ||
| }, | ||
| plugins: ['jest'] | ||
| }; |
| 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 |
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,59 @@ | ||
| '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 | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| fs.readFile(path.join(__dirname, 'form.html'), 'utf8', (err, content) => { | ||
| if (err) { | ||
| res.writeHead(500); | ||
| res.end('Error loading form'); | ||
|
|
||
| return; | ||
| } | ||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(content); | ||
| }); | ||
| } else if (req.method === 'POST' && req.url === '/add-expense') { | ||
| let data = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| try { | ||
| const expense = JSON.parse(data); | ||
|
|
||
| if (!expense.date || !expense.title || !expense.amount) { | ||
| res.writeHead(400, { 'Content-Type': 'application/json' }); | ||
|
|
||
| res.end( | ||
| '"Missing required fields: date, title, amount are required"', | ||
| ); | ||
|
Comment on lines
+34
to
+39
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 error response here is not a valid JSON object. You should send a JSON object like |
||
|
|
||
| return; | ||
| } | ||
|
|
||
| const filePath = path.join(__dirname, '..', 'db', 'expense.json'); | ||
|
|
||
| fs.writeFileSync(filePath, JSON.stringify(expense, null, 2)); | ||
|
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. Critical Issue: This line overwrites the entire |
||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(expense)); | ||
| } catch (err) { | ||
| res.writeHead(500, { 'Content-Type': 'application/json' }); | ||
| res.end('"Failed to process request"'); | ||
|
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 error response for a failed request is not a valid JSON object. It should be '{ "error": "Failed to process request" }' for proper JSON parsing by clients. 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 error response here is not a valid JSON object. You should send a JSON object like |
||
| } | ||
| }); | ||
| } else { | ||
| res.writeHead(404); | ||
| res.end(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Expense Form</title> | ||
| <style> | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| max-width: 600px; | ||
| margin: 0 auto; | ||
| padding: 20px; | ||
| } | ||
| form { | ||
| background: #f9f9f9; | ||
| padding: 20px; | ||
| border-radius: 8px; | ||
| box-shadow: 0 0 10px rgba(0,0,0,0.1); | ||
| } | ||
| .form-group { | ||
| margin-bottom: 15px; | ||
| } | ||
| label { | ||
| display: block; | ||
| margin-bottom: 5px; | ||
| font-weight: bold; | ||
| } | ||
| input { | ||
| width: 100%; | ||
| padding: 8px; | ||
| border: 1px solid #ddd; | ||
| border-radius: 4px; | ||
| box-sizing: border-box; | ||
| } | ||
| button { | ||
| background-color: #4CAF50; | ||
| color: white; | ||
| padding: 10px 20px; | ||
| border: none; | ||
| border-radius: 4px; | ||
| cursor: pointer; | ||
| } | ||
| button:hover { | ||
| background-color: #45a049; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <form id="expenseForm"> | ||
| <div class="form-group"> | ||
| <label for="date">Date:</label> | ||
| <input type="date" id="date" name="date" required> | ||
| </div> | ||
| <div class="form-group"> | ||
| <label for="title">Title:</label> | ||
| <input type="text" id="title" name="title" required> | ||
| </div> | ||
| <div class="form-group"> | ||
| <label for="amount">Amount:</label> | ||
| <input type="text" id="amount" name="amount" required> | ||
| </div> | ||
| <button type="submit">Save Expense</button> | ||
| </form> | ||
|
|
||
| <script> | ||
| document.getElementById('expenseForm').addEventListener('submit', async (e) => { | ||
| e.preventDefault(); | ||
|
|
||
| const formData = { | ||
| date: document.getElementById('date').value, | ||
| title: document.getElementById('title').value, | ||
| amount: document.getElementById('amount').value | ||
| }; | ||
|
|
||
| try { | ||
| const response = await fetch('/add-expense', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify(formData) | ||
| }); | ||
|
|
||
| const result = await response.json(); | ||
| if (response.ok) { | ||
| alert('Expense saved successfully!'); | ||
| } else { | ||
| alert(`Error: ${result}`); | ||
|
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. Minor issue: The error alert displays the entire 'result' object, which may not be user-friendly. Consider displaying a specific error message, such as 'result.error', if available, for clearer feedback to the user. 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 error alert displays the entire |
||
| } | ||
| } catch (err) { | ||
| alert('Error saving expense'); | ||
| } | ||
| }); | ||
| </script> | ||
| </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 error response for missing fields is not a valid JSON object. It should be something like '{ "error": "Missing required fields: date, title, amount are required" }' to ensure clients can parse it as JSON.