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
4 changes: 2 additions & 2 deletions db/expense.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"amount": "100",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The value for 'amount' is a string ("100"). If the task requires 'amount' to be a number, you should remove the quotes so it becomes: 100. Please check the requirements or checklist for the correct type.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The 'amount' field is currently stored as a string ("100"). According to the requirements, it should be stored as a number (e.g., 100 without quotes). Please update this value to be a number.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The 'amount' field is currently stored as a string ("100"). According to the requirements, it should be stored as a number (100). Please remove the quotes around the value to ensure it is saved as a number.

"date": "2024-01-25",
"title": "Test Expense",
"amount": "100"
"title": "Test Expense"
}
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
78 changes: 76 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
'use strict';

const http = require('http');
const path = require('path');
const fs = require('fs');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server((req, res) => {
if (req.method === 'GET' && req.url === '/') {
const filePath = path.join(__dirname, 'index.html');

fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(400);
res.end('Server error');

return;
}

res.setHeader('Content-Type', 'text/html');
res.end(content);
});

return;
}

if (req.method === 'POST' && req.url === '/add-expense') {
let body = '';

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

req.on('end', () => {
let fields;

try {
fields = JSON.parse(body);
} catch (err) {
res.statusCode = 400;

return res.end('Invalid JSON');
}

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

if (!fields.date || !fields.title || !fields.amount) {
res.statusCode = 400;

return res.end('Missing required fields');
}

const expense = {
amount: Number(fields.amount),
date: fields.date,
title: fields.title,
};

fs.writeFile(expensePath, JSON.stringify(expense, null, 2), (err) => {
if (err) {
res.statusCode = 500;

return res.end('Failed to save');
}

res.writeHead(200, { 'Content-Type': 'application/json' });

res.end(JSON.stringify(expense));
});
});

return;
}

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

return server;
}

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>Form Data</title>
</head>
<body>
<h1>Your expenses:</h1>
<form method="POST" action="/add-expense">
<label>Enter date:
<input type="date" name="date" required />
</label>
<br><br>
<label>Enter title:
<input type="text" name="title" required />
</label>
<br><br>
<label>Enter amount:
<input type="number" name="amount" required />
</label>
<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Loading