Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/examples/example-ejs-with-some-options.js
107 views
1
'use strict'
2
3
const fastify = require('fastify')()
4
const resolve = require('node:path').resolve
5
const templatesFolder = 'templates'
6
const data = { text: 'Hello from EJS Templates' }
7
8
fastify.register(require('..'), {
9
engine: {
10
ejs: require('ejs')
11
},
12
defaultContext: {
13
header: 'header value defined as default context',
14
footer: 'footer value defined as default context'
15
},
16
includeViewExtension: true,
17
layout: 'layout',
18
templates: templatesFolder,
19
options: {
20
filename: resolve(templatesFolder)
21
},
22
charset: 'utf-8' // sample usage, but specifying the same value already used as default
23
})
24
25
fastify.get('/', (_req, reply) => {
26
// reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type
27
reply.view('index-linking-other-pages', data)
28
})
29
30
fastify.get('/include-test', (_req, reply) => {
31
reply.view('index-with-includes', data)
32
})
33
34
fastify.get('/include-one-include-missing-test', (_req, reply) => {
35
reply.view('index-with-includes-one-missing', data)
36
})
37
38
fastify.get('/include-one-attribute-missing-test', (_req, reply) => {
39
reply.view('index-with-includes-and-attribute-missing', data)
40
})
41
42
fastify.listen({ port: 3000 }, err => {
43
if (err) throw err
44
console.log(`server listening on ${fastify.server.address().port}`)
45
})
46
47