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

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, {
'Content-Type': 'text/html',
});

res.end(`
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Add expense</title>
</head>
<body>
<h1>Add expense</h1>

<form method="POST" action="/add-expense">
<label>
Date
<input type="date" name="date" />
</label>
<br />
<label>
Title
<input type="text" name="title" />
</label>
<br />
<label>
Amount
<input type="text" name="amount" />
</label>
<br />
<button type="submit">Submit</button>
</form>
</body>
</html>
`);

return;
}

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

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

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

try {
if (contentType.includes('application/json')) {
expense = JSON.parse(body);
} else {
expense = querystring.parse(body);
}
} catch (error) {
res.writeHead(400, {
'Content-Type': 'text/plain',
});
res.end('Invalid request body');

return;
}

const { date, title, amount } = expense;

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

return;
}

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

fs.writeFileSync(dataPath, JSON.stringify({ date, title, amount }));

if (contentType.includes('application/json')) {
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify({ date, title, amount }));

return;
}

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

res.end(`
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Expense saved</title>
</head>
<body>
<h1>Expense saved</h1>
<pre>${escapeHtml(JSON.stringify({ date, title, amount }, null, 2))}</pre>
<a href="/">Back</a>
</body>
</html>
`);
});

return;
}

res.writeHead(404, {
'Content-Type': 'text/plain',
});
res.end('Page not found');
});
}

function escapeHtml(text) {
return text
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#39;');
}

module.exports = {
Expand Down
Loading