Skip to content
Open
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
17 changes: 12 additions & 5 deletions db/expense.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"date": "2024-01-25",
"title": "Test Expense",
"amount": "100"
}
[
{
"date": "1212-12-12",
"title": "kk",
"amount": "12"
},
{
"date": "1111-11-11",
"title": "hh",
"amount": "11"
}
]
8 changes: 4 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.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
87 changes: 85 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,91 @@
/* eslint-disable no-console */
'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) => {
const { method, url } = req;

if (url === '/' && method === 'GET') {
const filePath = path.join(__dirname, 'index.html');

fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500);

return res.end('Server error');
}

res.writeHead(200, {
'Content-type': 'text/html',
});
res.end(content);
});
} else if (url === '/submit-expense' && method === 'POST') {
let body = '';

req.on('data', (chunk) => {
body += chunk.toString();
});

req.on('end', () => {
const params = new URLSearchParams(body);
const newExpense = {
date: params.get('date'),
title: params.get('title'),
amount: params.get('amount'),
};

const dbPath = path.join(__dirname, '../db', 'expense.json');

fs.readFile(dbPath, 'utf-8', (err, data) => {
let expenses = [];

if (!err && data) {
try {
const parsedData = JSON.parse(data);

expenses = Array.isArray(parsedData) ? parsedData : [parsedData];
} catch (parseErr) {
expenses = [];
}
}

expenses.push(newExpense);

const jsonToSave = JSON.stringify(expenses, null, 2);

fs.writeFile(dbPath, jsonToSave, (writeErr) => {
if (writeErr) {
res.writeHead(500);

return res.end('Error saving to the file');
}

res.writeHead(200, { 'Content-type': 'text/html' });

res.end(`
<!DOCTYPE html>
<html>
<body>
<h1>Expense Saved Successfully</h1>
<p>Here is the saved data:</p>
<pre style="background: #f4f4f4; padding: 10px; border: 1px solid #ddd;">${jsonToSave}</pre>
<a href="/">Add another expense</a>
</body>
</html>
`);
});
});
});
} else {
res.statusCode = 404;
res.end('Invalid url');
}
});
}

module.exports = {
Expand Down
26 changes: 26 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Expense Tracker</title>
</head>
<body>
<h1>Add New Expense</h1>
<form action="/submit-expense" method="POST">
<div>
<label for="date">Date:</label>
<input type="date" name="date" required>
</div>
<div>
<label for="title">Title:</label>
<input type="text" name="title" required>
</div>
<div>
<label for="amount">Amount:</label>
<input type="number" name="amount" required>
</div>
<button type="submit">Save Expense</button>
</form>
</body>
</html>
Loading