-
Notifications
You must be signed in to change notification settings - Fork 218
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 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 |
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,118 @@ | ||
| '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 === 'object' && | ||
| typeof expense.date === 'string' && | ||
| typeof expense.title === 'string' && | ||
| typeof expense.amount === 'string' && | ||
| expense.date.trim() && | ||
| expense.title.trim() && | ||
| expense.amount.trim() | ||
| ); | ||
| } | ||
|
|
||
| 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" /> | ||
| <meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
| <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 contentType = String(req.headers['content-type'] || ''); | ||
| const expense = parseBody(rawBody, contentType); | ||
|
|
||
| if (!isValidExpense(expense)) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Invalid expense data'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFileSync(dataPath, JSON.stringify(expense, null, 2)); | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(expense)); | ||
|
Comment on lines
+106
to
+107
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 response here is raw JSON. According to the task description, you need to respond with an HTML page. 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 new expense. The requirement is to add the new expense to the existing ones.You'll need to first read the file's content (e.g., with
fs.readFileSync), parse the JSON into an array, push the newexpenseinto that array, and then write the updated array back to the file.