Performance: Testing POST with request body
This concept affects server response time and client perceived latency when handling POST requests with body data.
const request = require('supertest'); const app = require('./app'); describe('POST /api/data', () => { it('should respond with 200', async () => { const res = await request(app) .post('/api/data') .send({ name: 'John', age: 30 }) .set('Content-Type', 'application/json'); expect(res.statusCode).toBe(200); }); });
const request = require('supertest'); const app = require('./app'); describe('POST /api/data', () => { it('should respond with 200', async () => { const res = await request(app) .post('/api/data') .send('name=John&age=30') .set('Content-Type', 'application/x-www-form-urlencoded'); expect(res.statusCode).toBe(200); }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Raw URL-encoded string body | N/A | N/A | N/A | [X] Bad |
| JSON object body with correct Content-Type | N/A | N/A | N/A | [OK] Good |
app.use(express.json());
app.post('/user', (req, res) => {
res.json({ received: req.body.name });
});
request(app)
.post('/user')
.send({ name: 'Alice' })
.then(res => console.log(res.body));app.post('/data', (req, res) => {
res.json(req.body);
});
request(app)
.post('/data')
.send({ key: 'value' })
.expect(200, { key: 'value' });app.use(express.json());
app.post('/items', (req, res) => {
res.json({ count: req.body.length });
});Pass the test and this pattern moves unknown