-
Notifications
You must be signed in to change notification settings - Fork 240
final code for review #140
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?
Changes from 3 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 |
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,77 @@ | ||
| 'use strict'; | ||
|
|
||
| const { Server } = require('node:http'); | ||
| const path = require('node:path'); | ||
| const fs = require('node:fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| const htmlPath = path.join(__dirname, 'index.html'); | ||
|
|
||
| if (fs.existsSync(htmlPath)) { | ||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(fs.readFileSync(htmlPath)); | ||
| } else { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('File html not found'); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'POST' && req.url === '/add-expense') { | ||
| const expensePath = path.join(__dirname, '../', 'db', 'expense.json'); | ||
|
|
||
| let data = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| try { | ||
| const parsedData = JSON.parse(data); | ||
|
|
||
| if (!parsedData.date || !parsedData.title || !parsedData.amount) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid data format'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let expenseData = {}; | ||
|
|
||
| try { | ||
| const fileData = fs.readFileSync(expensePath, 'utf-8'); | ||
|
|
||
| expenseData = JSON.parse(fileData); | ||
| } catch { | ||
| expenseData = {}; | ||
| } | ||
|
|
||
| expenseData = { ...parsedData }; | ||
|
|
||
| fs.writeFileSync(expensePath, JSON.stringify(expenseData, 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. Ensure that you are writing an array to the Consider changing this line to: |
||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify(expenseData, null, 2)); | ||
| } catch (err) { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end(`Server error: ${err}`); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Not Found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <!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"> | ||
| <label for="date">Date:</label> | ||
| <input type="date" id="date" name="date" required /><br /> | ||
|
|
||
| <label for="title">Title:</label> | ||
| <input type="text" id="title" name="title" required /><br /> | ||
|
|
||
| <label for="amount">Amount:</label> | ||
| <input type="number" id="amount" name="amount" required /><br /> | ||
|
|
||
| <button type="submit">Submit</button> | ||
| </form> | ||
|
|
||
| <script> | ||
| document.querySelector('form').addEventListener('submit', async function (e) { | ||
| e.preventDefault(); | ||
|
|
||
| const formData = new FormData(this); | ||
| const data = Object.fromEntries(formData.entries()); | ||
|
|
||
| const response = await fetch('/add-expense', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(data), | ||
| }); | ||
|
|
||
| if (response.ok) { | ||
| alert(`Дані успішно додані! ${response}`); | ||
| } else { | ||
| alert('Помилка: ' + (await response.text())); | ||
| } | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| async function request() { | ||
| const options = { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| date: '2024-01-25', | ||
| title: 'Test Expense', | ||
| amount: '100', | ||
| }), | ||
| }; | ||
|
|
||
| try { | ||
| const response = await fetch('http://localhost:5701/add-expense', options); | ||
|
|
||
| if (!response.ok) { | ||
| return; | ||
| } | ||
|
|
||
| const data = await response.json(); // обробка відповіді як JSON | ||
|
|
||
| return data; | ||
| } catch (err) { | ||
| throw new Error(`catch error request: ${err}`); | ||
| } | ||
| } | ||
|
|
||
| request(); | ||
|
|
||
| module.exports = { request }; |
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 current implementation overwrites the entire
expenseDataobject with the newparsedData. To store multiple expenses, you should read the existing data fromexpense.json(if any), append the new expense to it, and then write the updated array back to the file. This is essential for accumulating expense records instead of just keeping the last one.Consider changing this line to something like:
expenseData = Array.isArray(expenseData) ? [...expenseData, parsedData] : [parsedData];to ensure you're appending to an array (or creating one if it doesn't exist).