Templating & Assets
Static Assets
Serve files from a directory (like ./public) automatically.
javascript
app.ASSETS('./public');This feature handles:
- Mime Types
- Caching (ETag, Last-Modified)
- Compression
- Range Requests (video streaming support)
Sending Files Manually
You can send a specific file from any route handler.
javascript
app.GET('/download/report', async (req, res) => {
await res.sendFile('./data/report.pdf', {
maxAge: 3600, // 1 hour cache
headers: { 'Content-Disposition': 'attachment' }
});
});Templating (Nunjucks)
Routerling integrates Nunjucks for server-side rendering.
Setup
javascript
app.TEMPLATES('./views', {
autoescape: true,
noCache: process.env.NODE_ENV !== 'production'
});Rendering
Use res.render (async) or res.renders (sync).
javascript
app.GET('/home', async (req, res) => {
await res.render('home.html', {
title: 'Welcome',
user: 'John Doe'
});
});Template Context
Templates automatically receive:
req: The request objectres: The response objectctx: The context objectctx.flash: Flash messages (if used)