-
Notifications
You must be signed in to change notification settings - Fork 240
done #147
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?
done #147
Changes from 1 commit
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 |
|---|---|---|
| @@ -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 |
| 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"} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,56 @@ | ||
| '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) => { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (url.pathname === '/' && req.method === 'GET') { | ||
| fs.createReadStream(path.resolve('public', 'index.html')).pipe(res); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (url.pathname === '/add-expense' && req.method === 'POST') { | ||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| const dataPath = path.resolve(__dirname, '..', 'db/expense.json'); | ||
| const data = Buffer.concat(chunks).toString(); | ||
|
|
||
| if (Object.keys(JSON.parse(data)).length !== 3) { | ||
|
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. Potential issue: JSON.parse(data) may throw an error if the incoming data is not valid JSON. You should wrap this in a try-catch block to handle invalid JSON and respond with a 400 status code and an appropriate error message. 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. Issue: There is no error handling for invalid JSON input. If the request body is not valid JSON, 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. You are parsing JSON data before the try-catch block. If the input is not valid JSON, this will throw an exception and crash the server. Move this check inside the try-catch block to handle errors gracefully. This is a critical issue related to error handling. |
||
| res.statusCode = 400; | ||
| res.setHeader('Content-type', 'text/plain'); | ||
| res.end('All params must be completed'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFileSync(dataPath, data); | ||
|
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. Issue: The use of fs.writeFileSync(dataPath, data) will overwrite the entire expense.json file with the new data. If the intention is to store multiple expenses, you should read the existing file, parse it as an array, append the new expense, and then write the updated array back to the file. This is a logic error related to how expenses are stored. 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. Issue: You are using 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. Issue: There is no error handling for file operations. If the file or directory does not exist, or if there is a write error, the server will throw and crash. You should handle file system errors gracefully and respond with an appropriate error message. |
||
| res.statusCode = 200; | ||
| res.setHeader('Content-type', 'application/json'); | ||
| res.end(data); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Page not found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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.
Potential issue: The code assumes that the db/expense.json file and its parent directory exist. If they do not, fs.writeFileSync will throw an error. Consider checking for their existence and creating them if necessary before writing.