Skip to content
Open

done #147

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
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
6 changes: 1 addition & 5 deletions db/expense.json
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"date": "2024-01-25",
"title": "Test Expense",
"amount": "100"
}
{}
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.1",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
52 changes: 52 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
'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) => {
const url = new URL(req.url, `http://${req.headers.host}`);

if (url.pathname === '/' && req.method === 'GET') {
fs.createReadStream(path.resolve('public', 'index.html')).pipe(res);

return;
}

if (url.pathname === '/add-expense' && req.method === 'POST') {
const chunks = [];

req.on('data', (chunk) => {
chunks.push(chunk);
});

req.on('end', () => {
if (url.pathname !== '/add-expense') {
req.end('Page not found');
}

const dataPath = path.resolve(__dirname, '..', 'db/expense.json');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue: The code assumes that the db/expense.json file and its parent directory exist. If they do not, fs.writeFileSync will throw an error. Consider checking for their existence and creating them if necessary before writing.

const data = Buffer.concat(chunks).toString();

if (Object.keys(JSON.parse(data)).length !== 3) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential issue: JSON.parse(data) may throw an error if the incoming data is not valid JSON. You should wrap this in a try-catch block to handle invalid JSON and respond with a 400 status code and an appropriate error message.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: There is no error handling for invalid JSON input. If the request body is not valid JSON, JSON.parse(data) will throw and crash the server. You should wrap this in a try-catch block and respond with a 400 error if parsing fails.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are parsing JSON data before the try-catch block. If the input is not valid JSON, this will throw an exception and crash the server. Move this check inside the try-catch block to handle errors gracefully. This is a critical issue related to error handling.

res.statusCode = 400;
res.setHeader('Content-type', 'text/plain');
res.end('All params must be completed');

return;
}

fs.writeFileSync(dataPath, data);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The use of fs.writeFileSync(dataPath, data) will overwrite the entire expense.json file with the new data. If the intention is to store multiple expenses, you should read the existing file, parse it as an array, append the new expense, and then write the updated array back to the file. This is a logic error related to how expenses are stored.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: You are using fs.writeFileSync(dataPath, data); to write the incoming expense data directly to the file. This overwrites the entire expense.json file with only the latest expense, instead of appending it to a list of expenses. According to the task requirements, you should append the new expense to an array in the JSON file, not replace the file contents. Please update this logic to read the existing expenses, append the new one, and then write the updated array back to the file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: There is no error handling for file operations. If the file or directory does not exist, or if there is a write error, the server will throw and crash. You should handle file system errors gracefully and respond with an appropriate error message.

res.statusCode = 200;
res.setHeader('Content-type', 'application/json');
res.end(data);
});

return;
}

res.statusCode = 404;
res.end('Page not found');
});

return server;
}

module.exports = {
Expand Down