WebApp resources
Static resources are stored in the /resource
folder.
Generating URLs
URLs to resources are generated using the getResourceUrl
function which is available when rendering templates.
// generates URL to 'info.pdf' stored in '/resource'
<img src="<%= getResourceUrl('info.pdf') %>" />
URL pattern: /webapp-files/<webappname>/<webappversion>/info.pdf
// generates URL to 'image.png' stored in '/resource/images'
<img src="<%= getResourceUrl('images/image.png') %>">
URL pattern: /webapp-files/<webappname>/<webappversion>/images/image.png
AppResource
The AppResource utility is used to access resources within a WebApp.
// retrieve an instance of the appResource utility
var appResource = require('appResource');
Resources can be accessed in three different ways, where pathToResource
is the path to the file in the resource folder.
appResource.getContent(pathToResource)
Returns the resource as a UTF-8 based string.
appResource.getNode(pathToResource)
Returns the resource as a JCR Node (sv:image or sv:file).
appResource.getInputStream(pathToResource)
Returns the resource as a java.io.BufferedInputStream.
(function() {
'use strict';
const router = require('router');
const appResource = require('appResource');
router.get('/', function(req, res) {
const fileContentAsString = appResource.getContent('message.txt');
const fileAsNode = appResource.getNode('pdf/info.pdf');
res.render('/', {message: fileContentAsString});
});
}());