-
Notifications
You must be signed in to change notification settings - Fork 235
feat: node form data task #191
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 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 |
|---|---|---|
| @@ -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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Node Form Data</title> | ||
| </head> | ||
| <body> | ||
| <form method="POST" action="/add-expense"> | ||
| <div> | ||
| <label for="date">Date:</label> | ||
| <input type="date" id="date" name="date" required> | ||
| </div> | ||
| <div> | ||
| <label for="title">Title:</label> | ||
| <input type="text" id="title" name="title" required> | ||
| </div> | ||
| <div> | ||
| <label for="amount">Amount:</label> | ||
| <input type="number" id="amount" name="amount" step="0.01" required> | ||
| </div> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,101 @@ | ||
| 'use strict'; | ||
|
|
||
| const { Server } = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const mime = require('mime-types'); | ||
| const querystring = require('querystring'); | ||
|
|
||
| const dbPath = path.resolve(__dirname, '../db/expense.json'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| try { | ||
| const url = new URL(req.url || '', `http://${req.headers.host}`); | ||
| const requestedPath = url.pathname.slice(1) || 'index.html'; | ||
| const realPath = path.join('public', requestedPath); | ||
| const mimeType = mime.contentType(path.extname(realPath)) || 'text/plain'; | ||
|
|
||
| if (requestedPath === 'add-expense') { | ||
| if (req.method === 'POST') { | ||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| req.on('end', () => { | ||
| const body = Buffer.concat(chunks).toString(); | ||
| const contentType = req.headers['content-type'] || ''; | ||
| const isJSON = contentType.includes('application/json'); | ||
| const { date, title, amount } = isJSON | ||
| ? JSON.parse(body) || {} | ||
| : querystring.parse(body) || {}; | ||
|
|
||
| if (!date || !title || !amount) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Invalid form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFileSync( | ||
| dbPath, | ||
| JSON.stringify({ date, title, amount }, null, 2), | ||
| ); | ||
|
Comment on lines
+43
to
+46
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 logic overwrites the To fix this, you need to:
Author
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. @mateacademy-ai-mentor
|
||
|
|
||
| if (isJSON) { | ||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify({ date, title, amount })); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
|
|
||
| res.end(` | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Expense saved</title> | ||
| </head> | ||
| <body> | ||
| <h1>Expense saved</h1> | ||
| <pre>${JSON.stringify({ date, title, amount }, null, 2)}</pre> | ||
| <a href="/">Back</a> | ||
| </body> | ||
| </html> | ||
| `); | ||
| }); | ||
| } else { | ||
| res.statusCode = 400; | ||
| res.end('Wrong method!'); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!fs.existsSync(realPath)) { | ||
| res.statusCode = 404; | ||
| res.end('Not Found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const dataStream = fs.createReadStream(realPath); | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', mimeType); | ||
| dataStream.pipe(res); | ||
| } catch (error) { | ||
| res.statusCode = 500; | ||
| res.end('Server Error'); | ||
| } | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.