Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
6 changes: 1 addition & 5 deletions db/expense.json
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{
"date": "2024-01-25",
"title": "Test Expense",
"amount": "100"
}
{"date":"2024-01-25","title":"Test Expense","amount":"100"}
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
33 changes: 31 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
'use strict';

const http = require('http');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/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 task requires showing an HTML form. There is no GET handler to serve the form (e.g. GET '/' returning an HTML page with inputs for date, title and amount). Add a route that serves the form HTML.

let body = '';

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

req.on('end', () => {
const expense = JSON.parse(body);
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 code uses JSON.parse(body), which assumes the client sends JSON. An HTML form will typically submit application/x-www-form-urlencoded. Detect Content-Type and parse accordingly (for urlencoded use querystring.parse), and wrap parsing in try/catch to avoid crashing on invalid input.


if (expense.date && expense.title && expense.amount) {
const fs = require('fs');
const path = require('path');
const dataPath = path.resolve(__dirname, '../db/expense.json');

fs.writeFileSync(dataPath, JSON.stringify(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.

When writing the expense to file you use JSON.stringify(expense). If you want the saved file to be readable/use well formatted JSON, use JSON.stringify(expense, null, 2) to include indentation.

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expense));
Comment on lines +72 to +73
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 response for a successful save is sent with Content-Type: application/json and raw JSON. The requirement asks to "return an HTML page with well formatted JSON" — change the response to text/html and embed pretty-printed JSON (for example JSON.stringify(expense, null, 2)) inside an HTML structure such as <pre>...</pre>.

Comment on lines +72 to +73
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 successful POST response currently sends 'Content-Type: application/json' and raw JSON. The spec requires 'Content-Type: text/html' and an HTML document embedding JSON.stringify(expense, null, 2) (for example inside a

) so the browser shows formatted JSON.

Comment on lines +71 to +73
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After writing the expense file the handler returns application/json. Per checklist items #5, #11 and #16 the successful POST /add-expense response must use Content-Type: 'text/html' and return an HTML document that embeds JSON.stringify(expense, null, 2) (for example inside a <pre>). Replace this JSON response with an HTML document containing the pretty-printed JSON.

} else {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(['Missing required fields']));
Comment on lines +75 to +76
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Error responses (400/404) are returned as application/json. Consider returning HTML pages for errors or at least consistent responses with the task (the main success response should be an HTML page). Also provide clearer error information when required fields are missing.

Comment on lines +74 to +76
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 'missing required fields' branch responds with application/json. Change this to an HTML response (Content-Type: text/html) that explains which required fields are missing (or at least a clear human-readable message) to match the app's main behavior.

Comment on lines +74 to +76
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 "missing required fields" branch returns JSON. Per checklist item #7 change this to an HTML page (Content-Type: 'text/html; charset=utf-8') that clearly explains which fields are missing (for example: <p>Missing required fields: date, title, amount</p>). Consider computing the missing field names from expense and inserting them into the message.

}
});
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
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 fallback 404 response uses application/json. Make 404 responses consistent with the rest of the app by returning an HTML page (Content-Type: text/html) with a friendly 'Not Found' message.

}
});
}

module.exports = {
Expand Down
Loading