Performance: Creating a basic Express server
This affects the initial page load speed and server response time, impacting how quickly the browser receives content to render.
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World'); }); app.listen(3000);
const express = require('express'); const app = express(); app.use((req, res, next) => { // heavy synchronous task for(let i = 0; i < 1e9; i++) {} next(); }); app.get('/', (req, res) => { res.send('Hello World'); }); app.listen(3000);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous middleware | N/A (server-side) | N/A | N/A | [X] Bad |
| Minimal async route handler | N/A (server-side) | N/A | N/A | [OK] Good |
app.listen() in an Express server?app.listen()app.get() define routes, but do not start the server. Importing Express or sending responses are done elsewhere.app.listen() starts server [OK]app.listen() sends responsesimport express from 'express';.require() is CommonJS style, not modern ES modules. var express = import('express'); is invalid syntax. load is not a valid keyword.import [OK]http://localhost:3000/hello given this Express code?import express from 'express';
const app = express();
app.get('/hello', (req, res) => {
res.send('Hello World!');
});
app.listen(3000);/hello, the server matches the route and sends the defined response.import express from 'express';
const app = express();
app.get('/', (req, res) => {
res.send('Welcome!');
});
app.listen();app.listen() method requires a port number to start the server. Here, no port is given.res.send() is correct, and Express is imported properly.app.listen() needs port [OK]/data and listens on port 8080. Which code snippet correctly does this?app.get('/data', ...) is correct.res.json({ message: 'Hello JSON' }) is correct. The server must listen on port 8080.Pass the test and this pattern moves unknown