Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<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>
<h1>Form data for expense</h1>
<form action="/add-expense" method="POST">
<label for="date">Date:</label><br>
<input
type="date"
name="date"
id="date"
required
/>

<br><br>

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

<br><br>

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

<br><br>

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

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

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

server.on('request', (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);

if (url.pathname === '/' && req.method === 'GET') {
fs.createReadStream(path.resolve('public', 'index.html')).pipe(res);

return;
}

if (url.pathname === '/add-expense' && req.method === 'POST') {
const chunks = [];

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

req.on('end', () => {
const expensePath = path.resolve(__dirname, '..', 'db/expense.json');
const data = Buffer.concat(chunks).toString();
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 POST handler assumes the request body is JSON. However, standard HTML forms submit data as application/x-www-form-urlencoded. You need to parse the body accordingly, or ensure the form sends JSON. This is a critical requirement for correct form handling.


if (Object.keys(JSON.parse(data)).length !== 3) {
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 code only checks that there are 3 keys in the parsed data, but does not check that the required fields (date, title, amount) are present and valid. The requirements specify that missing or invalid form data should be handled gracefully.

res.statusCode = 400;
res.setHeader('Content-type', 'text/plain');
res.end('All params must be completed');

return;
}

fs.writeFileSync(expensePath, 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.

There is no check to ensure the db directory exists before writing the file. If the directory does not exist, this will throw an error. The requirements specify that the app must create the directory/file if they do not exist.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no error handling for file operations (e.g., if writing fails). The requirements specify that file errors must be handled gracefully, with an appropriate response to the client.

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.

After saving, the response is sent as raw JSON with content type application/json. The requirements specify that the server must return an HTML page displaying the saved JSON in a well-formatted way (e.g., inside a <pre> tag), not just the JSON itself.

res.end(data);
});

return;
}

res.statusCode = 404;
res.end('Page not found');
});

return server;
}

module.exports = {
Expand Down