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
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
'use strict';

const { createServer } = require('./createServer');
const createServer = require('./createServer');

createServer().listen(5701, () => {
console.log(`Server is running on http://localhost:${5701} 🚀`);
Expand Down
59 changes: 57 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
const fileStream = fs.createReadStream('src/index.html');

res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
fileStream.pipe(res);

fileStream.on('error', () => {
res.statusCode = 500;

res.end('Server error');
});

return;
}

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

req.on('data', (chunck) => (body += chunck));

req.on('end', () => {
const data = JSON.parse(body);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

HTML forms submitted with the default enctype send data as application/x-www-form-urlencoded, not as a JSON string. Using JSON.parse() on this data will result in an error. You should use a tool like Node's built-in querystring module or the URLSearchParams class to parse the form data correctly.

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 body of a standard form submission is in x-www-form-urlencoded format (e.g., key1=value1&key2=value2), not JSON. Using JSON.parse() here will cause a syntax error when the form is submitted. You need to parse this URL-encoded string. Consider using Node's built-in querystring module or the URLSearchParams class.

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 HTML form sends data in the x-www-form-urlencoded format (like date=...&title=...), not as a JSON string. Using JSON.parse() on this format will cause a syntax error.

You should use a different method to parse this kind of data. Consider using Node's built-in querystring module or the URLSearchParams class.


// eslint-disable-next-line
console.log(body);
// eslint-disable-next-line
console.log(data);

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

return;
}

const expense = {
date: data.date,
title: data.title,
amount: data.amount,
};

fs.writeFileSync('db/expense.json', JSON.stringify(expense));

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expense));
Comment on lines +51 to +52
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 returning an 'HTML page with well formatted JSON'. Your implementation returns a raw JSON response. You need to change the Content-Type to text/html and construct an HTML string that includes the JSON data, perhaps inside a <pre> tag to preserve formatting.

Comment on lines +51 to +52
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 returning an HTML page that displays the saved JSON data. Currently, you're sending a raw JSON response with Content-Type: application/json. You should set the Content-Type to text/html and construct an HTML string to send back. A good way to display the JSON is inside <pre> tags to preserve formatting.

Comment on lines +51 to +52
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 server must return an HTML page that displays the saved data, not a raw JSON response.

You'll need to change the Content-Type to text/html and send back an HTML string that includes the formatted JSON data (e.g., by wrapping JSON.stringify(expense, null, 2) inside a <pre> tag).

});

return;
}

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

module.exports = {
Expand Down
32 changes: 32 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form
action="/add-expense"
method="POST"
enctype="application/x-www-form-urlencoded"
>
<div>
<p>Date</p>
<input type="date" name="date" />
</div>
<div>
<p>Title</p>
<input type="text" name="title" />
</div>
<div>
<p>Amount</p>
<input type="number" name="amount" step="0.01" />
</div>

<div style="margin-top: 5px">
<button type="submit">Submit</button>
</div>
</form>
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 request body from an HTML form with enctype="application/x-www-form-urlencoded" is not a JSON string. It's a URL-encoded string (e.g., key1=value1&key2=value2). Using JSON.parse() here will cause a crash.

You need to parse this specific format. Consider using Node.js's built-in querystring module or the URLSearchParams class.

</body>
</html>
Loading