I am trying to create a plugin for serving my static files in HapiJS. My file structure is the following
|_ plugins
| |_ assets
| |_ assets.js
| |_ package.json
| |_ views
|_ public
| |_ css
|app.js
| config.json
| package.json
Within my plugins/assets/assets.js file I have the following code:
const Inert = require('inert');
exports.register = function(server, options, next) {
server.register('inert');
server.route({
method: 'GET',
path: '/{public}',
handler: {
directory: {path: '../../public'}
}
});
next();
};
exports.register.attributes = {
pkg: require('./package');
}
Within my config.json file (glue manifest):
{
"server": {
"app": {
"slogan": "We push the web forward"
}
},
"connections": [
{
"port": 3000,
"labels": ["web-ui"]
}
],
"registrations": [
{"plugin": "vision"},
{"plugin": "inert"},
{"plugin": "glue"},
{"plugin": "./plugins/assets"}
]
}
And finally, in my app.js:
var Hapi = require('hapi');
var Glue = require('glue');
var manifest = require('./config.json');
const options = {
relativeTo: __dirname
};
Glue.compose(manifest, options, function (err, server) {
server.start(function(err) {
console.log('Server running');
});
});
My question is this the correct way to serve static files using a plugins approach with glue? All the examples I have seen do not use a plugin approach and instead use the server.register() within the app.js file to serve the static files from the public directory.
via Vince
No comments:
Post a Comment