Skip to content
Open
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
70 changes: 70 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
/* eslint-disable no-console */
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class

const server = new http.Server();

server.on('request', (req, res) => {

Choose a reason for hiding this comment

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

The request handler is missing a crucial part of the requirements. The application must also 'show an HTML form'. You should add logic here to handle GET requests to serve this form to the user.

if (req.method === 'POST' && req.url === '/add-expense') {
let body = '';

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

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

try {
fields = JSON.parse(body);

Choose a reason for hiding this comment

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

This parsing method isn't correct for the data sent by the HTML form. Forms using POST send data in application/x-www-form-urlencoded format (like date=...&title=...), which is not valid JSON. JSON.parse() will throw an error here.

You'll need to parse this string differently. Consider using Node's built-in URLSearchParams to handle this format.

} catch (err) {
res.statusCode = 400;

return res.end('Invalid JSON');
}

if (!fields.amount || !fields.date || !fields.title) {
res.statusCode = 400;

return res.end('Missing required fields');
}

const filePath = 'db/expense.json';

const newData = {
amount: fields.amount,
date: fields.date,
title: fields.title,
};

fs.writeFileSync(filePath, JSON.stringify(newData));

const file = fs.readFileSync(filePath);

res.setHeader('Content-Type', 'application/json');
res.end(file);
Comment on lines +50 to +51

Choose a reason for hiding this comment

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

The response for a successful submission needs two changes to meet the requirements:

  1. The Content-Type header should be text/html.
  2. The response body should be an HTML document that displays the saved JSON, not the raw JSON data itself. For example: res.end(
    ${file}
    );

});

return;
}

if (req.url !== '/' && req.url !== '/add-expense') {

Choose a reason for hiding this comment

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

This condition correctly routes GET / requests to show the form, but it also allows GET /add-expense to do the same, which is likely not intended. It's generally better to handle each route explicitly. For instance, you could check for req.method === 'GET' && req.url === '/' to serve the form, and then let other unhandled routes result in a 404.

res.statusCode = 404;

return res.end('Invalid Url');
}

res.setHeader('Content-type', 'text/html');

res.end(
`<form method="POST" action="/add-expense">
<input name="date" type="date">
<input name="title" type="text">
<input name="amount" type="number">
<button type="submit">Submit</button>
</form>`,
);
});

return server;
}

module.exports = {
Expand Down