Skip to content
Open

task #192

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
25 changes: 25 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>form Data Server</title>
</head>
<body>
<form id="transferForm"
action="/add-expense"
method="post"
enctype="application/x-www-form-urlencoded">
<label for="date">Date:</label>
<input type="date" id="date" name="date" required />

<label for="title">Title:</label>
<input type="text" id="title" name="title" required />

<label for="amount">Amount:</label>
<input type="number" id="amount" name="amount" step="0.01" required />

<button type="submit">Submit</button>
</form>
</body>
</html>
84 changes: 82 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,88 @@
'use strict';

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

function parseExpense(body, contentType = '') {
// It's for tests, because axios sends JSON instead of x-www-form-urlencoded.
if (contentType.includes('application/json')) {
return JSON.parse(body);
}

return Object.fromEntries(new URLSearchParams(body).entries());
}

function isValidExpense(expense) {
return Boolean(expense.date && expense.title && expense.amount);
}

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
if (req.url === '/') {
fs.readFile(
path.resolve(__dirname, '../public/index.html'),
'utf8',
(err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Bad Request');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
},
);
} else if (req.url === '/add-expense' && req.method === 'POST') {
const body = [];

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

req.on('end', () => {
let parsedData;

try {
parsedData = parseExpense(
Buffer.concat(body).toString(),
req.headers['content-type'],
);
} catch (err) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Invalid request body');

return;
}

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

return;
}

fs.writeFile(
path.resolve(__dirname, '../db/expense.json'),
JSON.stringify(parsedData, null, 2),
(err) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error saving data');
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(parsedData));
Comment on lines +73 to +74
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 requirements, the server should return an HTML page that displays the well-formatted JSON data. Currently, you are returning a raw JSON response with the Content-Type set to application/json. The content type should be text/html, and the response body should be an HTML document containing the JSON.

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

return server;
}

module.exports = {
Expand Down
Loading