diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 0000000..bb13dfc --- /dev/null +++ b/.github/workflows/test.yml-template @@ -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 diff --git a/db/expense.json b/db/expense.json index 1bc75a6..1257e7a 100644 --- a/db/expense.json +++ b/db/expense.json @@ -1,5 +1 @@ -{ - "date": "2024-01-25", - "title": "Test Expense", - "amount": "100" -} \ No newline at end of file +{"date":"2024-01-25","title":"Test Expense","amount":"100"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 28a4d31..dcf56b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "2.1.2", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", @@ -1468,10 +1468,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.2.tgz", + "integrity": "sha512-gUXFdqqOfYzF9R3RSx2pCa5GLdOkxB9bFbF+dpUpzucdgGAANqOGdqpmNnMj+e3xA9YHraUWq3xo9cwe5vD9pQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", diff --git a/package.json b/package.json index 8a92721..2a221db 100644 --- a/package.json +++ b/package.json @@ -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.2", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000..e353607 Binary files /dev/null and b/public/favicon.ico differ diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..f485d4a --- /dev/null +++ b/public/index.html @@ -0,0 +1,30 @@ + + + + + + + + Form Data + + +
+

Expenses Tracker

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/public/style.css b/public/style.css new file mode 100644 index 0000000..43dbfd9 --- /dev/null +++ b/public/style.css @@ -0,0 +1,67 @@ +body { + padding: 0; + margin: 0; +} + +.form { + display: flex; + gap: 1em; + + align-items: flex-end; +} + +.header { + display: flex; + padding: 0.5em; + + border: 2px dashed black; + border-radius: 16px; + + max-width: fit-content; +} + +.container { + padding-top: 6em; + padding-left: 5em; + + box-sizing: border-box; + display: flex; + flex-direction: column; + gap: 3em; + width: 100vw; + height: 100vh; + background-color: lemonchiffon; + + color: black; + font-family: Arial, Helvetica, sans-serif; + font-weight: 500; + font-size: 18px; + line-height: 140%; +} + +.input-wrapper { + display: flex; + flex-direction: column; + gap: 0.5em; +} + +.button-send { + display: flex; + justify-content: center; + align-items: center; + padding: 8px; + + max-height: fit-content; + + background-color: green; + border: none; + color: white; + border-radius: 10px; + + transition: all 0.2s ease; +} + +.button-send:hover { + cursor: pointer; + background-color: lightgreen; +} diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..8c3a463 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,119 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + +const BASE_PUBLIC_DIR = path.resolve('public'); + +function sendBadRequest(res) { + res.statusCode = 400; + res.end('Bad request'); +} + +function sendStaticFiles(rawUrl, res) { + const normalizedUrl = rawUrl.slice(1) || 'index.html'; + const filePath = path.resolve(BASE_PUBLIC_DIR, normalizedUrl); + + if (!fs.existsSync(filePath)) { + res.statusCode = 404; + res.end('File not found!'); + + return; + } + + const fileStream = fs.createReadStream(filePath); + + fileStream.pipe(res); + + fileStream.on('error', () => { + res.statusCode = 500; + res.end('Error during reading file'); + }); + + fileStream.on('close', () => { + fileStream.destroy(); + }); +} + +function handleRawFormData(formdata, res) { + if (!formdata) { + sendBadRequest(res); + + return; + } + + const dataToParams = new URLSearchParams(formdata); + + if ( + !dataToParams.get('date') || + !dataToParams.get('title') || + !dataToParams.get('amount') + ) { + sendBadRequest(res); + + return; + } + + return Object.fromEntries(dataToParams); +} + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = new http.Server(); + + server.on('request', (req, res) => { + res.statusCode = 200; + + if (req.method === 'GET') { + sendStaticFiles(req.url, res); + + return; + } + + if (req.method === 'POST' && req.url === '/add-expense') { + res.setHeader('Content-type', 'application/json'); + + const chunks = []; + let rawData = ''; + + req.on('data', (chunk) => { + chunks.push(chunk); + }); + + req.on('end', () => { + rawData = Buffer.concat(chunks).toString(); + + let preparedData = ''; + + // extra logic: in tests cases requests are in application/json, + // but IRL, formdata usually comes as application/x-form-encoded + // hope that MA guys will re-write this task (as well as previous one) + const contentTypeHeaders = req.headers['content-type'] || ''; + + if (contentTypeHeaders.includes('application/json')) { + preparedData = JSON.parse(rawData); + } else { + preparedData = handleRawFormData(rawData, res); + } + + if (!preparedData.date || !preparedData.title || !preparedData.amount) { + sendBadRequest(res); + + return; + } + + fs.writeFile( + path.resolve('db/expense.json'), + JSON.stringify(preparedData), + () => { + res.end(JSON.stringify(preparedData)); + }, + ); + }); + } + }); + + return server; } module.exports = {