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
84 changes: 79 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,84 @@
'use strict';

const http = require('http');
const fs = require('fs/promises');
const path = require('path');
const { text } = require('stream/consumers');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer(async (req, res) => {
const { method, url } = req;

if (url === '/' && method === 'GET') {
await serveHomePage(res);

return;
}

if (url === '/add-expense' && method === 'POST') {
await processExpenseSubmission(req, res);

return;
}

sendErrorResponse(res, 404, 'Invalid URL');
});
}

async function serveHomePage(res) {
const htmlFilePath = path.join(__dirname, 'public', 'index.html');

try {
const htmlContent = await fs.readFile(htmlFilePath, 'utf8');

res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(htmlContent);
} catch (error) {
sendErrorResponse(res, 500, 'Error loading HTML');
}
}

async function processExpenseSubmission(req, res) {
try {
const requestBodyText = await text(req);
const expenseData = parseExpenseData(requestBodyText);

const { date, title, amount } = expenseData;

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

return;
}

const databaseFilePath = path.join(__dirname, '..', 'db', 'expense.json');

await fs.writeFile(databaseFilePath, JSON.stringify(expenseData), 'utf8');

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expenseData));
} catch (error) {
sendErrorResponse(res, 500, 'Server Error');
}
}

function parseExpenseData(bodyText) {
try {
return JSON.parse(bodyText);
} catch {
const formData = new URLSearchParams(bodyText);

return {
date: formData.get('date'),
title: formData.get('title'),
amount: formData.get('amount'),
};
}
}

function sendErrorResponse(res, statusCode, errorMessage) {
res.writeHead(statusCode, { 'Content-Type': 'text/plain' });
res.end(errorMessage);
}

module.exports = {
createServer,
};
module.exports = { createServer };
87 changes: 87 additions & 0 deletions src/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Form Data</title>
</head>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f6f8;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}

h1 {
color: #333;
margin-bottom: 20px;
}

form {
background-color: #ffffff;
padding: 20px 40px;
border-radius: 12px;
width: 100%;
max-width: 400px;
display: flex;
flex-direction: column;
gap: 10px;
}

label {
font-weight: 600;
margin-bottom: 5px;
color: #444;
}

input {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 8px;
transition:
border-color 0.2s ease-in-out,
box-shadow 0.2s ease-in-out;
}

input:focus {
border-color: #4a90e2;
box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
outline: none;
}

button {
padding: 10px;
font-size: 16px;
background-color: #4a90e2;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.2s ease-in-out;
}

button:hover {
background-color: #357ab7;
}
</style>
<body>
<h1>Form Data</h1>
<form action="/add-expense" method="post">
<label>Date</label>
<input type="date" name="date" required />
<br />
<label>Title</label>
<input type="text" name="title" required />
<br />
<label>Amount</label>
<input type="number" name="amount" required />
<br />
<button type="submit">Submit</button>
</form>
</body>
</html>