Performance: res.render for templates
This affects server response time and how quickly the browser receives the fully rendered HTML page.
app.get('/', async (req, res) => { // Use async data fetching const data = await fetchDataAsync(); res.render('index', { data }); });
app.get('/', (req, res) => { // Render template with heavy synchronous logic const data = heavySyncDataProcessing(); res.render('index', { data }); });
| Pattern | Server CPU Usage | Response Size | Response Time | Verdict |
|---|---|---|---|---|
| Heavy synchronous data processing before res.render | High CPU blocking | Medium | Slow response, blocks event loop | [X] Bad |
| Async data fetching with minimal data passed to res.render | Low CPU non-blocking | Small | Fast response, non-blocking | [OK] Good |
res.render do in an Express app?res.render is used to create a web page by combining a template with data.app.get('/', (req, res) => {
res.render('index', { name: 'Alice' });
});
Assuming the 'index' template contains: <h1>Hello, <%= name %>!</h1>name in the template.<%= name %> to insert the value 'Alice' into the HTML.app.get('/about', (req, res) => {
res.render('about', title: 'About Us');
});{ title: 'About Us' } to pass variables properly.user = { name: 'Bob', age: 30 }. Which code correctly renders the template passing the user data so the template can access name and age directly?name and age directly, not inside a nested object.{ name: user.name, age: user.age } makes name and age available directly in the template.Pass the test and this pattern moves unknown