-
Notifications
You must be signed in to change notification settings - Fork 240
Implement form-data server #179
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 |
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,115 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const dataPath = path.resolve(__dirname, '../db/expense.json'); | ||
|
|
||
| function readBody(req) { | ||
| return new Promise((resolve) => { | ||
| let body = ''; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| body += chunk; | ||
| }); | ||
|
|
||
| req.on('end', () => resolve(body)); | ||
| }); | ||
| } | ||
|
|
||
| function parseBody(rawBody, contentType = '') { | ||
| if (!rawBody) { | ||
| return null; | ||
| } | ||
|
|
||
| if (contentType.includes('application/json')) { | ||
| try { | ||
| return JSON.parse(rawBody); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| if (contentType.includes('application/x-www-form-urlencoded')) { | ||
| const params = new URLSearchParams(rawBody); | ||
| const obj = {}; | ||
|
|
||
| for (const [key, value] of params.entries()) { | ||
| obj[key] = value; | ||
| } | ||
|
|
||
| return obj; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function isValidExpense(expense) { | ||
| return ( | ||
| expense && | ||
| typeof expense.date === 'string' && | ||
| typeof expense.title === 'string' && | ||
| typeof expense.amount === 'string' && | ||
| expense.date && | ||
| expense.title && | ||
| expense.amount | ||
| ); | ||
| } | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer(async (req, res) => { | ||
| const { method, url } = req; | ||
|
|
||
| if (method === 'GET' && url === '/') { | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/html'); | ||
|
|
||
| res.end(`<!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>Add expense</title> | ||
| </head> | ||
| <body> | ||
| <h1>Add expense</h1> | ||
| <form method="POST" action="/add-expense"> | ||
| <label>Date <input type="date" name="date" required></label><br> | ||
| <label>Title <input type="text" name="title" required></label><br> | ||
| <label>Amount <input type="text" name="amount" required></label><br> | ||
| <button type="submit">Save</button> | ||
| </form> | ||
| </body> | ||
| </html>`); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (method === 'POST' && url === '/add-expense') { | ||
| const rawBody = await readBody(req); | ||
| const parsed = parseBody(rawBody, req.headers['content-type'] || ''); | ||
|
|
||
| if (!isValidExpense(parsed)) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Invalid expense data'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFileSync(dataPath, JSON.stringify(parsed, 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 |
||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(parsed)); | ||
|
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 requirement is to 'return an HTML page with well formatted JSON'. Currently, you are returning a response with 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 task requires returning an HTML page after submission, not raw JSON. The |
||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Not found'); | ||
| }); | ||
| } | ||
|
|
||
| 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.
This line overwrites the
expense.jsonfile with only the latest submission. To save all expenses, you should first read the existing data from the file, add the new expense to the collection (likely an array), and then write the entire collection back to the file.