| Level Navigation: 1 | 2 | 3 | (4ℹ️) | (5ℹ️) | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14⚡ | 15⚡ | (16ℹ️) | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26⚡ | 27⚡ | 28⚡ | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39⚡ | 40⚡ |
Now that you’ve tested with plain text, let’s serve HTML from your root route. Update your route to use res.send() with an HTML snippet so you can see the browser render formatted content. For example, try sending <h1>Hello Express!</h1><p>Your server is working!</p> to see HTML rendering in action.
// src/index.js
app.get('/', (req, res) => {
res.send('<h1>Hello Express!</h1><p>Your server is working!</p>');
});
When serving HTML, you might want to write multi-line HTML for better readability. JavaScript template literals (strings wrapped in backticks `) allow you to write multi-line strings and include variables:
Backticks vs. Quotes:
' or double " quotes): Must be on a single line`): Can span multiple lines and support interpolation with ${variable}Example:
// Single-line string (regular quotes)
res.send('<h1>Hello Express!</h1><p>Your server is working!</p>');
// Multi-line string (backticks)
res.send(`
<h1>Hello Express!</h1>
<p>Your server is working!</p>
<ul>
<li>Feature 1</li>
<li>Feature 2</li>
</ul>
`);
// Template literal with variable interpolation
const name = 'Express';
res.send(`<h1>Hello ${name}!</h1>`);
When to use backticks:
Note: When using backticks for multi-line HTML, whitespace (spaces, tabs, newlines) is preserved. This is usually fine for HTML since browsers collapse extra whitespace, but be aware that indentation in your code will appear in the output.