Skip to content
Open

sol #204

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
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
66 changes: 64 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
'use strict';

const http = require('http');

const fs = require('fs');

const querystring = require('querystring');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Typo in form action: '/add-exspense' should be '/add-expense' to match the server route on line 23. The form will submit to a non-existent endpoint, triggering a 404 response.

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.url === '/' && req.method === 'GET') {
fs.readFile('./src/index.html', (err, date) => {
if (err) {
res.statusCode = 500;
res.end();

return;
}
res.setHeader('Content-Type', 'text/html');
res.statusCode = 200;
res.end(date);
});
} else if (req.url === '/add-expense' && req.method === 'POST') {
// обробити дані
let body = '';

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

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

if (contentType.includes('application/json')) {
parsed = JSON.parse(body);
} else {
parsed = querystring.parse(body);
}

if (!parsed.date || !parsed.title || !parsed.amount) {
res.statusCode = 400;
res.end('Bad request');

return;
}

const data = JSON.stringify(parsed, null, 2);

fs.writeFile('./db/expense.json', data, (error) => {
if (error) {
res.statusCode = 500;
res.end();

return;
}

res.setHeader('Content-Type', 'text/html');
res.statusCode = 200;
res.end(`<html><body><pre>${data}</pre></body></html>`);
});
});
} else {
res.statusCode = 404;
res.end();
}
});
}

module.exports = {
Expand Down
21 changes: 21 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Compression App</title>
</head>
<body>
<form method="POST" action="/add-expense">
<div>
<label for="date">Date</label>
<input type="date" name="date">
<label for="title">Title</label>
<input type="text" name="title">
<label for="amount">Amount</label>
<input type="number" name="amount">
</div>

<button type="submit">submit</button>
</form>
</body>
</html>
Loading