Skip to content
Open

done #147

Show file tree
Hide file tree
Changes from all 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"
}
{"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
67 changes: 67 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
'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;
}

try {
JSON.parse(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.

Parsing the JSON here is redundant since you already did it above (and incorrectly, outside the try-catch). You should parse the JSON only once, inside the try-catch block, and reuse the parsed object for validation and further processing.

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

fs.writeFile(dataPath, data, (err) => {
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 overwriting the entire expense.json file with the new data. According to the requirements, you should append the new expense to an array of expenses, not replace the file contents. This is a critical logic error that breaks the core functionality of the expense tracker.

if (err) {
res.statusCode = 500;

return res.end('Error saving data');
}
// fs.writeFileSync(dataPath, data);
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