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
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.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
72 changes: 70 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
'use strict';

const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const querystring = require('node:querystring');

const DB_PATH = path.resolve(__dirname, '../db/expense.json');
const FORM_PATH = path.resolve(__dirname, './views/form.html');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
const html = fs.readFileSync(FORM_PATH, 'utf-8');

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

return;
}

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

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

req.on('end', () => {
const contentType = req.headers['content-type'] || '';
let expense;

if (contentType.includes('application/x-www-form-urlencoded')) {
expense = querystring.parse(body);
} else {
try {
expense = JSON.parse(body);
} catch {
expense = querystring.parse(body);
}
}

const { date, title, amount } = expense;

if (!date || !title || !amount) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request: date, title and amount are required');

return;
}

const data = { date, title, amount };

fs.writeFileSync(DB_PATH, JSON.stringify(data));

const html = `<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>Expense Saved</title></head>
<body>
<pre>${JSON.stringify(data, null, 2)}</pre>
</body>
</html>`;

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

return;
}

res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
});
}

module.exports = {
Expand Down
28 changes: 28 additions & 0 deletions src/views/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Add Expense</title>
</head>
<body>
<h1>Add Expense</h1>
<form action="/add-expense" method="POST">
<label>
Date:
<input type="date" name="date" required />
</label>
<br />
<label>
Title:
<input type="text" name="title" required />
</label>
<br />
<label>
Amount:
<input type="number" name="amount" required />
</label>
<br />
<button type="submit">Submit</button>
</form>
</body>
</html>
Loading