-
Notifications
You must be signed in to change notification settings - Fork 240
Node form data solution #151
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?
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 @@ | ||
| { | ||
| "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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link | ||
| rel="stylesheet" | ||
| type="text/css" | ||
| href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/versions/bulma-no-dark-mode.min.css" | ||
| /> | ||
| <title>Form data</title> | ||
| </head> | ||
| <body> | ||
| <div class="container is-max-tablet"> | ||
| <section class="hero"> | ||
| <div class="hero-body"> | ||
| <p class="title">Form data</p> | ||
| <p class="subtitle">Add an expense</p> | ||
| </div> | ||
| </section> | ||
|
|
||
| <div class="box"> | ||
| <form | ||
| action="/add-expense" | ||
| method="POST" | ||
| enctype="application/x-www-form-urlencoded" | ||
| name="formData" | ||
| > | ||
| <div class="field"> | ||
| <label class="label">Date</label> | ||
| <div class="control"> | ||
| <input class="input" type="date" name="date" /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="field"> | ||
| <label class="label">Title</label> | ||
| <div class="control"> | ||
| <input | ||
| class="input" | ||
| type="text" | ||
| name="title" | ||
| placeholder="e.g. boxes" | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="field"> | ||
| <label class="label">Amount</label> | ||
| <div class="control"> | ||
| <input | ||
| class="input" | ||
| type="number" | ||
| name="amount" | ||
| placeholder="e.g. 5" | ||
| /> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div class="field is-grouped"> | ||
| <div class="control"> | ||
| <button class="button is-link" type="submit">Submit</button> | ||
| </div> | ||
| <div class="control"> | ||
| <button class="button is-link is-light" type="reset"> | ||
| Cancel | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,54 @@ | ||
| '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 expensePath = path.resolve(__dirname, '..', 'db/expense.json'); | ||
| const data = Buffer.concat(chunks).toString(); | ||
|
|
||
| if (Object.keys(JSON.parse(data)).length !== 3) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-type', 'text/plain'); | ||
| res.end('All params must be completed'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFile(expensePath, data); | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-type', 'application/json'); | ||
| res.end(data); | ||
| }); | ||
|
Comment on lines
+62
to
+66
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 using |
||
|
|
||
| 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.
Issue:
fs.writeFileis called without a callback function. This means any errors during file writing will be ignored, and the response may be sent before the file is actually written. To ensure reliability and proper error handling, provide a callback to handle possible errors and only send the response after the file operation completes.