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
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
16 changes: 16 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!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>
<form method="post" action="/submit-expense">
<input name="date" type="date">
<input placeholder="Title" name="title" type="text">
<input placeholder="Costs" name="amount" type="number">
<button type="submit">Submit</button>
</form>
</body>
</html>
71 changes: 69 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server();

server.on('request', (req, res) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the task description, the application should show an HTML form. The server is currently only handling POST requests to /add-expense. You need to add a handler for a GET request to serve the form to the user.

const baseUrl = req.url;
const dbPath = path.resolve('db', 'expense.json');
const mainPath = path.resolve('public', 'index.html');

if (baseUrl === '/') {
const stream = fs.createReadStream(mainPath);

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');

return res.pipe(stream);
}

if (baseUrl === '/add-expense') {
if (req.method !== 'POST') {
res.statusCode = 400;

return res.end();
}

const chunks = [];

req.on('data', (chunk) => {
chunks.push(chunk);
});

req.on('end', () => {
try {
const buffer = Buffer.concat(chunks).toString();
const data = JSON.parse(buffer);

if (!data.date || !data.title || !data.amount) {
fs.writeFileSync(dbPath, JSON.stringify({}));

res.statusCode = 400;
res.setHeader('Content-Type', 'application/json');

return res.end(JSON.stringify('Missing fields'));
}

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

res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
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 task requires the server to "return an HTML page with well formatted JSON". To do that, the Content-Type header should be set to text/html.


return res.end(JSON.stringify(data));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This sends back a raw JSON response. Per the requirements, you should send back an HTML document that contains the formatted JSON data. For example, you could wrap the JSON string in <pre> tags within a basic HTML structure.

} catch (e) {
res.statusCode = 400;
res.setHeader('Content-Type', 'application/json');

return res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});

return;
}

res.statusCode = 404;
res.end();
});

return server;
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions tests/formDataServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Form Data Server', () => {
});

it('should save data for valid expense on "POST /submit-expense" request', async () => {
fs.writeFileSync(dataPath, JSON.stringify({}));
fs.writeFileSync(dataPath, JSON.stringify([]));

const expense = {
date: '2024-01-25',
Expand All @@ -57,7 +57,7 @@ describe('Form Data Server', () => {
});

it('should reject request without all params on "POST /submit-expense" request', async () => {
fs.writeFileSync(dataPath, JSON.stringify({}));
fs.writeFileSync(dataPath, JSON.stringify([]));

const expense = {
title: 'Invalid Expense',
Expand Down
Loading