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
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.1",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
74 changes: 74 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
type="text/css"
href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/versions/bulma-no-dark-mode.min.css"
/>
<title>Form data</title>
</head>
<body>
<div class="container is-max-tablet">
<section class="hero">
<div class="hero-body">
<p class="title">Form data</p>
<p class="subtitle">Add an expense</p>
</div>
</section>

<div class="box">
<form
action="/add-expense"
method="POST"
enctype="application/x-www-form-urlencoded"
name="formData"
>
<div class="field">
<label class="label">Date</label>
<div class="control">
<input class="input" type="date" name="date" />
</div>
</div>

<div class="field">
<label class="label">Title</label>
<div class="control">
<input
class="input"
type="text"
name="title"
placeholder="e.g. boxes"
/>
</div>
</div>

<div class="field">
<label class="label">Amount</label>
<div class="control">
<input
class="input"
type="number"
name="amount"
placeholder="e.g. 5"
/>
</div>
</div>

<div class="field is-grouped">
<div class="control">
<button class="button is-link" type="submit">Submit</button>
</div>
<div class="control">
<button class="button is-link is-light" type="reset">
Cancel
</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
77 changes: 75 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,81 @@
'use strict';
const http = require('http');
const fs = require('fs');
const querystring = require('querystring');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer(async (req, res) => {
if (req.url === '/') {
if (req.method === 'GET') {
res.setHeader('Content-type', 'text/html');
const file = fs.createReadStream('public/index.html');

file
.on('error', () => {
res.statusCode = 500;
res.end('Failed to read index html');
})
.pipe(res);

res.on('close', () => {
file.destroy();
});

return;
}

res.statusCode = 405;
res.end('Method not allowed');
return;
}

if (req.url === '/add-expense') {
if (req.method !== 'POST') {
res.statusCode = 405;
res.end('Method not allowed!');
return;
}
const chunks = [];

for await (const chunk of req) {
chunks.push(chunk);
}

const rawData = Buffer.concat(chunks).toString();
let data;

if (req.headers['content-type'] === 'application/json') {
data = JSON.parse(rawData);
} else {
data = querystring.parse(rawData);
}

const jsonData = JSON.stringify(data);
const { title, amount, date } = data;

if (!title || !amount || !date) {
res.statusCode = 404;
res.end('Not valid form!');
return;
}

fs.writeFileSync('db/expense.json', jsonData, () => {
res.statusCode = 500;
res.end('Failed to write expense.json');
return;
});
Comment on lines +62 to +66
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You are using fs.writeFileSync with a callback, but the synchronous version does not accept a callback and will throw on error. This is a critical issue: switch to using fs.writeFile (the async version) with a callback to properly handle file write errors and only send the response after the write completes. This is necessary to ensure data integrity and proper error handling as required by the task.


res.setHeader('Content-type', 'application/json');
res.end(jsonData);
return;
}

res.statusCode = 404;
res.end('Not found');
return;
});

return server;
}

module.exports = {
Expand Down