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

const { Server } = require('node:http');
const path = require('node:path');
const { pipeline } = require('node:stream');
const { createReadStream, createWriteStream } = require('node:fs');

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

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

if (pathname === '/' || pathname === '/index.html') {
const indexPath = path.resolve(__dirname, 'index.html');
const readStream = createReadStream(indexPath);

pipeline(readStream, res, (err) => {
if (err) {
sendResponse(res, 500, 'Error reading index.html');
}
});

return;
}

if (pathname === '/add-expense') {
if (req.method === 'GET') {
return sendResponse(
res,
400,
'GET requests are not allowed on /submit-expense',
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 error message refers to '/submit-expense', but the route being handled is '/add-expense'. This may be confusing for users and is likely a mistake. Please update the error message to refer to '/add-expense'.

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 error message here says 'GET requests are not allowed on /submit-expense', but the correct endpoint is '/add-expense'. This could confuse users and should be updated to match the actual route.

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 error message references /submit-expense, but the correct endpoint is /add-expense. Please update the message to reference /add-expense as required by the task.

);
}

let body = '';

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

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

try {
fields = JSON.parse(body);
} catch (err) {
return sendResponse(res, 400, 'Invalid JSON');
}

const { date, title, amount } = fields;

if (!date || !title || !amount) {
return sendResponse(res, 400, 'Missing required fields');
}

const jsonPath = path.resolve(__dirname, '..', 'db', 'expense.json');

try {
const writeStream = createWriteStream(jsonPath);

writeStream.on('error', () => {
return sendResponse(res, 500, 'Error saving expense data');
});

writeStream.write(JSON.stringify(fields), () => {
writeStream.end();
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify(fields));
});
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 logic will overwrite the entire 'expense.json' file with each new expense, rather than appending or merging new expense data. If the checklist requires storing multiple expenses, you should read the existing file, parse it as an array, add the new expense, and then write the updated array back to the file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Critical issue: This logic overwrites the entire 'expense.json' file with only the most recent expense. According to the requirements, you should maintain a list of expenses (i.e., append the new expense to an array in the JSON file, not replace it). Please update this section to read the existing expenses, add the new one, and then write the updated array back to the file.

} catch (err) {
sendResponse(res, 500, 'Error saving expense');
}
});

return;
}
sendResponse(res, 404, 'Not Found');
});

return server;
}

function sendResponse(res, statusCode, message) {
res.writeHead(statusCode, { 'content-type': 'text/plain' });
res.end(message);
}

module.exports = {
Expand Down
24 changes: 24 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form data</title>
</head>
<body>
<h1>Form Data</h1>
<form action="/submit-expense" method="post">
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 form's action is set to '/submit-expense', but your server expects POST requests at '/add-expense'. Update the action to '/add-expense' to match your server route.

<label for="data">Data:</label>
<input type="data" id="data" name="data"><br>
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 input type 'data' is not valid HTML. You likely meant 'date' for a date picker. Please change type="data" to type="date".


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

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

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