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

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

const HTML =
'<form method="post">' +
'<input type="text" name="title" />' +
'<input type="date" name="date" />' +
'<input type="number" name="amount" />' +
'<button type="submit" name="amount">submit</button>' +
'</form>';

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const pathname = url.pathname;

if (pathname !== '/submit-expense' && pathname !== '/') {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('404 Not Found');

return;
}

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

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

req.on('end', () => {
const body = Buffer.concat(chunks).toString();
const data = JSON.parse(body);

if (!data) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Invalid Request');

return;
}

const { date, title, amount } = data;

if (!date || !title || !amount) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Bad Request');

return;
}

fs.writeFile('db/expense.json', body);

res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');

Choose a reason for hiding this comment

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

According to the task requirements, the server should return an HTML page after a successful POST request. This Content-Type header should be set to text/html.

res.end(JSON.stringify(data));

Choose a reason for hiding this comment

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

The task requires returning an HTML page that contains the well-formatted JSON, not just the raw JSON data. You should wrap this JSON data within an HTML structure, for example using <pre> tags for formatting.

});

return;
}

res.setHeader('Content-Type', 'text/html');
res.end(HTML);
});
}

module.exports = {
Expand Down
10 changes: 5 additions & 5 deletions tests/formDataServer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { Server, Agent } = require('http');
// this prevents `socket hang up` for Node.js 20.10+
axios.defaults.httpAgent = new Agent({ keepAlive: false });

const PORT = 5701;
const PORT = 5703;
const HOST = `http://localhost:${PORT}`;

describe('Form Data Server', () => {
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('Form Data Server', () => {
title: 'Test Expense',
amount: '100',
};
const response = await axios.post(`${HOST}/add-expense`, expense);
const response = await axios.post(`${HOST}/submit-expense`, expense);

expect(response.status).toBe(200);

Expand All @@ -67,7 +67,7 @@ describe('Form Data Server', () => {
expect.assertions(2);

try {
await axios.post(`${HOST}/add-expense`, expense);
await axios.post(`${HOST}/submit-expense`, expense);
} catch (err) {
expect(err.response.data.length).toBeGreaterThan(0);

Expand All @@ -81,9 +81,9 @@ describe('Form Data Server', () => {
title: 'Test Expense',
amount: '100',
};
const response = await axios.post(`${HOST}/add-expense`, expense);
const response = await axios.post(`${HOST}/submit-expense`, expense);

expect(response.headers['content-type']).toBe('application/json');
expect(response.headers['content-type']).toBe('text/html');
expect(response.data).toStrictEqual(expense);

Choose a reason for hiding this comment

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

This assertion is now inconsistent with the Content-Type check on the line above. If the server correctly returns an HTML page, response.data will be a string containing HTML, not a plain JavaScript object. You should update this to check that the returned HTML string contains the well-formatted JSON data.

});

Expand Down
Loading