-
Notifications
You must be signed in to change notification settings - Fork 240
form-data #146
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?
form-data #146
Changes from 2 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 |
|---|---|---|
| @@ -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,5 @@ | ||
| { | ||
| "amount": "100", | ||
|
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 'amount' field is currently stored as a string ("100"). According to the requirements, it should be stored as a number (e.g., 100 without quotes). Please update this value to be a number. 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 'amount' field is currently stored as a string ("100"). According to the requirements, it should be stored as a number (100). Please remove the quotes around the value to ensure it is saved as a number. |
||
| "date": "2024-01-25", | ||
| "title": "Test Expense", | ||
| "amount": "100" | ||
| "title": "Test Expense" | ||
| } | ||
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,82 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new http.Server((req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| const filePath = path.join(__dirname, 'index.html'); | ||
|
|
||
| fs.readFile(filePath, (err, content) => { | ||
| if (err) { | ||
| res.writeHead(400); | ||
| res.end('Server error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.setHeader('Content-Type', 'text/html'); | ||
| res.end(content); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'POST' && req.url === '/add-expense') { | ||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body += chunk.toString(); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| let fields; | ||
|
|
||
| try { | ||
| fields = JSON.parse(body); | ||
| } catch (err) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Invalid JSON'); | ||
| } | ||
|
|
||
| const expensePath = path.join(__dirname, '..', 'db', 'expense.json'); | ||
|
|
||
| if (!fields.date || !fields.title || !fields.amount) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing required fields'); | ||
| } | ||
|
|
||
| const expense = { | ||
| amount: fields.amount, | ||
|
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 'amount' field is saved directly from the request body (fields.amount). If the requirements specify that 'amount' should be a number, you should convert it to a number (e.g., using Number(fields.amount)) before saving. Otherwise, it will be saved as a string if the client sends it as such. 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 'amount' field is being stored directly from the request body (fields.amount). According to the requirements, 'amount' must be stored as a number. Please convert 'fields.amount' to a number (e.g., using Number(fields.amount)) before saving it to the expense object. |
||
| date: fields.date, | ||
| title: fields.title, | ||
| }; | ||
|
|
||
| fs.writeFile(expensePath, JSON.stringify(expense, null, 2), (err) => { | ||
| if (err) { | ||
| res.statusCode = 500; | ||
|
|
||
| return res.end('Failed to save'); | ||
| } | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
|
|
||
| res.end(JSON.stringify(expense)); | ||
| }); | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.end('Not found'); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Form Data</title> | ||
| </head> | ||
| <body> | ||
| <h1>Your expenses:</h1> | ||
| <form method="POST" action="/add-expense"> | ||
| <label>Enter date: | ||
| <input type="date" name="date" required /> | ||
| </label> | ||
| <br><br> | ||
| <label>Enter title: | ||
| <input type="text" name="title" required /> | ||
| </label> | ||
| <br><br> | ||
| <label>Enter amount: | ||
| <input type="number" name="amount" required /> | ||
| </label> | ||
| <br><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 value for 'amount' is a string ("100"). If the task requires 'amount' to be a number, you should remove the quotes so it becomes: 100. Please check the requirements or checklist for the correct type.