-
Notifications
You must be signed in to change notification settings - Fork 240
Develop #157
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?
Develop #157
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,82 @@ | ||
| 'use strict'; | ||
|
|
||
| const { Server } = require('node:http'); | ||
| const path = require('node:path'); | ||
| const { pipeline } = require('node:stream'); | ||
| const { createReadStream } = require('node:fs'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new Server(); | ||
|
|
||
| server.on('request', async (req, res) => { | ||
| const { pathname } = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (pathname === '/' || pathname === '/index.html') { | ||
| const indexPath = path.resolve(__dirname, 'index.html'); | ||
| const readStream = createReadStream(indexPath); | ||
|
|
||
| pipeline(readStream, res, (err) => { | ||
| if (err) { | ||
| sendResponse(res, 500, 'Error reading index.html'); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (pathname === '/add-expense') { | ||
| if (req.method === 'GET') { | ||
| return sendResponse( | ||
| res, | ||
| 400, | ||
| 'GET requests are not allowed on /submit-expense', | ||
|
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. The error message here says 'GET requests are not allowed on /submit-expense', but the correct endpoint is '/add-expense'. This could confuse users and should be updated to match the actual route. 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. The error message references |
||
| ); | ||
| } | ||
|
|
||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body += chunk.toString(); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| let fields; | ||
|
|
||
| try { | ||
| fields = JSON.parse(body); | ||
| } catch (err) { | ||
| return sendResponse(res, 400, 'Invalid JSON'); | ||
| } | ||
|
|
||
| const { date, title, amount } = fields; | ||
|
|
||
| if (!date || !title || !amount) { | ||
| return sendResponse(res, 400, 'Missing required fields'); | ||
| } | ||
|
|
||
| const jsonPath = path.resolve(__dirname, '..', 'db', 'expense.json'); | ||
|
|
||
| try { | ||
| fs.writeFileSync(jsonPath, JSON.stringify(fields, null, 2)); | ||
|
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. This line overwrites the entire |
||
| res.writeHead(200, { 'content-type': 'application/json' }); | ||
| res.end(JSON.stringify(fields)); | ||
| } catch (err) { | ||
| sendResponse(res, 500, 'Error saving expense'); | ||
| } | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
| sendResponse(res, 404, 'Not Found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| function sendResponse(res, statusCode, message) { | ||
| res.writeHead(statusCode, { 'content-type': 'text/plain' }); | ||
| res.end(message); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Form data</title> | ||
| </head> | ||
| <body> | ||
| <h1>Form Data</h1> | ||
| <form action="/add-expense" method="post"> | ||
| <label for="date">Data:</label> | ||
| <input type="date" id="date" name="date"><br> | ||
|
|
||
| <label for="title">Title:</label> | ||
| <input type="text" id="title" name="title"><br> | ||
|
|
||
| <label for="amount">Amount:</label> | ||
| <input type="number" id="amount" name="amount"><br> | ||
|
|
||
| <button type="submit">Submit</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.
The error message refers to '/submit-expense', but the route being handled is '/add-expense'. This may be confusing for users and is likely a mistake. Please update the error message to refer to '/add-expense'.