-
Notifications
You must be signed in to change notification settings - Fork 236
solution #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
solution #187
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,90 @@ | ||
| '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 | ||
| return http.createServer((request, response) => { | ||
| if (request.url === '/' && request.method === 'GET') { | ||
| const htmlPath = path.resolve(__dirname, 'index.html'); | ||
|
|
||
| fs.readFile(htmlPath, 'utf-8', (error, data) => { | ||
| if (error) { | ||
| response.statusCode = 500; | ||
| response.end('Internal Server Error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| response.statusCode = 200; | ||
| response.setHeader('Content-Type', 'text/html'); | ||
| response.end(data); | ||
| }); | ||
| } else if (request.url === '/add-expense' && request.method === 'POST') { | ||
| const chunks = []; | ||
|
|
||
| request.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| request.on('end', () => { | ||
| try { | ||
| const bodyStr = Buffer.concat(chunks).toString(); | ||
| let parsedData; | ||
|
|
||
| const contentType = request.headers['content-type'] || ''; | ||
|
|
||
| if (contentType === 'application/json') { | ||
| parsedData = JSON.parse(bodyStr); | ||
| } else { | ||
| const params = new URLSearchParams(bodyStr); | ||
|
|
||
| parsedData = Object.fromEntries(params); | ||
| } | ||
|
|
||
| if (!parsedData.amount || !parsedData.title || !parsedData.date) { | ||
| response.statusCode = 400; | ||
| response.end('Invalid data'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const dbPath = path.join(__dirname, '../db/expense.json'); | ||
|
|
||
| fs.writeFile(dbPath, JSON.stringify(parsedData, null, 2), (error) => { | ||
| if (error) { | ||
| response.statusCode = 500; | ||
| response.end('Internal Server Error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const accept = request.headers.accept || ''; | ||
|
|
||
| if (accept.includes('text/html')) { | ||
| response.statusCode = 200; | ||
| response.setHeader('Content-Type', 'text/html'); | ||
|
|
||
| response.end(` | ||
| <pre>${JSON.stringify(parsedData, null, 2)}</pre> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job wrapping the JSON in |
||
| `); | ||
| } else { | ||
| response.statusCode = 200; | ||
| response.setHeader('Content-Type', 'application/json'); | ||
| response.end(JSON.stringify(parsedData)); | ||
| } | ||
| }); | ||
| } catch (error) { | ||
| response.statusCode = 400; | ||
| response.end('Invalid data'); | ||
| } | ||
| }); | ||
| } else { | ||
| response.statusCode = 404; | ||
| response.end('Not Found'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <form method="post" action="/add-expense"> | ||
| <input type="text" name="title" placeholder="Title"> | ||
| <input type="text" name="amount" placeholder="Amount"> | ||
| <input type="text" name="date" placeholder="Date"> | ||
| <button type="submit">Add Expense</button> | ||
| </form> | ||
| </body> | ||
| </html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
fs.writeFilehere overwrites the file with every new expense. The requirement is to save all expenses. You'll need to first read the contents ofdb/expense.json, parse it into an array, add the new expense to that array, and then write the entire updated array back to the file.