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

const { Server } = require('node:http');
const path = require('node:path');
const { pipeline } = require('node:stream');
const { createReadStream } = require('node:fs');
const fs = require('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 {
fs.writeFileSync(jsonPath, JSON.stringify(fields, null, 2));
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 line overwrites the entire expense.json file with the new expense object. According to the requirements, you should append each new expense to an array in the JSON file, not overwrite it. Please update this logic to read the existing array, add the new expense, and then write the updated array back to the file.

res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify(fields));
} 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="/add-expense" method="post">
<label for="date">Data:</label>
<input type="date" id="date" name="date"><br>

<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>