Supposing you are using Express can use the function express.Static() which serves precisely to "serve static files".
This function has the following structure:
express.static(root, [options])
You can use the native function __dirname
to reference the folder itself as relative path and so be able to concatenate with the rest of the path to its folder at higher levels or below.
The following example assumes that your project has the following folder structure:
root folder
|
|---- render
| |
| |---- views
|
|---- node_modules
// todo conteúdo da pasta views disponível via GET
app.use(express.static(__dirname + '/render/views'))
This function allows all files in the selected folder and all underlying directories to be available via a request GET
.
It also allows (optionally) define options as: etag
, maxAge
, the list of extensions, define headers
or even if navigation in the directory will be allowed or not... this is an example of function options in the documentation itself:
var options = {
dotfiles: 'ignore',
etag: false,
extensions: ['htm', 'html'],
index: false,
maxAge: '1d',
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now())
}
}
app.use(express.static('public', options))
If you don’t want to provide all the contents of the directory you can simply handle one or more specific files via request routing GET
example:
const fs = require('fs')
// ...
app.get('/index.ejs', (req, res, next) => {
res.type('text/plain')
.send(fs.readFileSync('./render/views/index.ejs', 'utf-8'))
.end()
})
Through routing it is important to define the mime-type
suitable... as "templates" .ejs
are not equal to files .js
should use only text/plain
.
Using this method it is possible to serve any file type, just add the mime-type
correct.
In his Service Worker would look something like:
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll([
'/',
'/index.ejs',
'../css/materialize.css',
'../css/custom.css', ....
Without having to back up or forward directories in the file’s path statement .ejs
.
Try and tell me if it worked.
Source: Express 4.x API
The process is the same. But the route to the file .ejs must be defined on the server via
GET
or if using Express as a static resource using express.Static() The question is: why cache a file that cannot be used by the browser?. Caching will not make the browser use it to render anything.– Lauro Moraes
Lauro Moraes, I know it won’t render, what I want to know is how to send this rendered folder (files) so that SW Chace can work with Mobile First. if it were in . html and you could do it in Aboa.
– HENNING SUMMER