Skip to content
Open

done #158

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
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"}
70 changes: 63 additions & 7 deletions package-lock.json

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

5 changes: 4 additions & 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.1",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand All @@ -27,5 +27,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"formidable": "^3.5.4"
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Form data (with Node.js)

Implement an app that

- shows an HTML form with an info about an expense (date, title and amount)
- receives its data in a POST request
- save it to a `db/expense.json` file
Expand Down
54 changes: 52 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server();

server.on('request', (req, res) => {
if (req.url !== '/add-expense') {
res.setHeader('Content-type', 'text/plain');
res.statusCode = 404;
res.end('Wrong request url');

return;
}

const chunks = [];

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

req.on('end', () => {
const text = Buffer.concat(chunks).toString();

const data = JSON.parse(text);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The server expects the request body to be JSON, but if the client sends data as application/x-www-form-urlencoded (which is common for HTML forms), JSON.parse(text) will throw an error. You need to handle both content types or ensure the client sends JSON as required by the task.


if (!data['date'] || !data['title'] || !data['amount']) {
res.statusCode = 400;
res.end('Not full data');

return;
}

const writeStream = fs.createWriteStream(path.resolve('db/expense.json'));
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 creates a write stream that will overwrite 'db/expense.json' with each new request. If the task requires storing multiple expenses, you should append the new expense to the existing data instead of overwriting the file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: Writing the incoming data directly to db/expense.json will overwrite any existing expenses. According to the requirements, you should store multiple expenses. You need to read the existing file, append the new expense, and write the updated array back.


writeStream.end(text);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no error handling for the write stream. If an error occurs while writing to the file, the client will not be informed. Consider adding an 'error' event handler for the write stream to handle such cases.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: There is no error handling for file write operations. If writing to the file fails, the client will not be notified. You should handle errors and respond with an appropriate status code and message.


writeStream.on('finish', () => {
res.setHeader('Content-type', 'application/json');
res.statusCode = 200;
res.end(text);
});
});

req.on('error', (error) => {
res.statusCode = 400;
res.end(`Request error: ${error}`);
});
});
server.on('error', () => {});

return server;
}

module.exports = {
Expand Down
17 changes: 17 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Add Expense</title>
</head>
<body>
<h1>Expense Form</h1>
<form action="http://localhost:5701/add-expense" method="POST">
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 form uses the default 'application/x-www-form-urlencoded' encoding, but your server expects JSON data in the request body. You need to either update the server to handle URL-encoded data or use JavaScript on the client side to send JSON via fetch/AJAX.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Issue: The form submits data as application/x-www-form-urlencoded by default, but your server expects JSON. This will cause the server to fail when parsing the request body. You need to either update the form to send JSON (using JavaScript and fetch/AJAX) or update the server to handle URL-encoded form data.

<label>Date: <input type="date" name="date" required /></label><br />
<label>Title: <input type="text" name="title" required /></label><br />
<label>Amount: <input type="number" name="amount" required /></label
><br />
<button type="submit">Submit</button>
</form>
</body>
</html>