-
Notifications
You must be signed in to change notification settings - Fork 218
solution #178
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?
solution #178
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 |
|---|---|---|
| @@ -1,8 +1,71 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
|
|
||
| const HTML = | ||
| '<form method="post">' + | ||
| '<input type="text" name="title" />' + | ||
| '<input type="date" name="date" />' + | ||
| '<input type="number" name="amount" />' + | ||
| '<button type="submit" name="amount">submit</button>' + | ||
| '</form>'; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
| const pathname = url.pathname; | ||
|
|
||
| if (pathname !== '/submit-expense' && pathname !== '/') { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('404 Not Found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (pathname === '/submit-expense' && req.method === 'POST') { | ||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on('end', () => { | ||
| const body = Buffer.concat(chunks).toString(); | ||
| const data = JSON.parse(body); | ||
|
|
||
| if (!data) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Invalid Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const { date, title, amount } = data; | ||
|
|
||
| if (!date || !title || !amount) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Bad Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.writeFile('db/expense.json', body); | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(data)); | ||
|
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 that contains the well-formatted JSON, not just the raw JSON data. You should wrap this JSON data within an HTML structure, for example using |
||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.setHeader('Content-Type', 'text/html'); | ||
| res.end(HTML); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ const { Server, Agent } = require('http'); | |
| // this prevents `socket hang up` for Node.js 20.10+ | ||
| axios.defaults.httpAgent = new Agent({ keepAlive: false }); | ||
|
|
||
| const PORT = 5701; | ||
| const PORT = 5703; | ||
| const HOST = `http://localhost:${PORT}`; | ||
|
|
||
| describe('Form Data Server', () => { | ||
|
|
@@ -47,7 +47,7 @@ describe('Form Data Server', () => { | |
| title: 'Test Expense', | ||
| amount: '100', | ||
| }; | ||
| const response = await axios.post(`${HOST}/add-expense`, expense); | ||
| const response = await axios.post(`${HOST}/submit-expense`, expense); | ||
|
|
||
| expect(response.status).toBe(200); | ||
|
|
||
|
|
@@ -67,7 +67,7 @@ describe('Form Data Server', () => { | |
| expect.assertions(2); | ||
|
|
||
| try { | ||
| await axios.post(`${HOST}/add-expense`, expense); | ||
| await axios.post(`${HOST}/submit-expense`, expense); | ||
| } catch (err) { | ||
| expect(err.response.data.length).toBeGreaterThan(0); | ||
|
|
||
|
|
@@ -81,9 +81,9 @@ describe('Form Data Server', () => { | |
| title: 'Test Expense', | ||
| amount: '100', | ||
| }; | ||
| const response = await axios.post(`${HOST}/add-expense`, expense); | ||
| const response = await axios.post(`${HOST}/submit-expense`, expense); | ||
|
|
||
| expect(response.headers['content-type']).toBe('application/json'); | ||
| expect(response.headers['content-type']).toBe('text/html'); | ||
| expect(response.data).toStrictEqual(expense); | ||
|
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 assertion is now inconsistent with the |
||
| }); | ||
|
|
||
|
|
||
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.
According to the task requirements, the server should return an HTML page after a successful POST request. This
Content-Typeheader should be set totext/html.