There are 4 different types of hooks in ZinkyJS:
To add a hook, use addHook() method after app initialization.
const Zinky = require('zinky');
var app = new Zinky({
aliases: {
'': 'home'
'customers': 'users'
}
});
app.addHook((req, res, next) => {
console.log("I'm a global hook. I run every time!")
next();
})
app.listen();
You've probably noticed the usual (req, res, next), well known by ExpressJS developers. But if it's the first time you see them don't worry, here is a short explanation:
You can copy the code to your project and see what it does.
To create a module's hook add hook(req, res, next) method to the module and you're done.
Test it, by adding this to "app_modules/users/index.js":
hook(req, res, next) {
console.log("Hello! I'm the users module hook. I run every time you request users module")
next();
}
Visit localhost:3000/users/, and open the terminal that's running the app to see the result.
You will see these messages:
I'm a global hook. I run every time!
Hello! I'm the users module hook. I run every time you request users module
Eg:
BEFORE_GET_root(req, res, next) {
console.log("Keep Ready! You're about to access operation");
next();
}
Add the above method to "app_modules/users/index.js", and visit localhost:3000/users/.
Check the terminal!
What we saw here, are internal BEFORE hooks, that means that they belongs to the module of the operation they're attached to. But since ZinkyJS is awesome, it gives us a way to attach a hook to a specific operation from outside its module. These are what we call external BEFORE hooks
External BEFORE hooks are created by writing methods named as following:
'BEFORE_' + operationModule + operation
Add this hook to "app_modules/home/index.js", and test it:
BEFORE_users_GET_root(req, res, next) {
console.log("I'm a before hook that lives at home");
next();
}
The AFTER hooks are called after the request has finished, which means that the client gets the request response before they are called.
And like BEFORE hooks, the AFTER hooks can be internal or external.
To see it in real add these lines to 'app_modules/users/index.js':
AFTER_GET_root(req, res) {
console.log("I'm the hook that runs after the client has received the response");
}
And these ones to "app_modules/home/index.js":
AFTER_users_GET_root(req, res) {
console.log("I'm called from home after client has been served");
}
Now run the app and visit localhost:3000/users/.
As you can see, AFTER hooks don't get a callback. That's because they are not called in a stack, ZinkyJS runs every one of them asynchronously and does not wait until the first finishes to call the second.
ExpressJS middlewares, when used in ZinkyJS, are used as global hooks and are added using addHook(). This method (addHook) works exactly like the use() method of ExpressJS.
In our example we'll use this basicauth-middleware that was developed originaly for ExpressJS but that we can use with ZinkyJS.
Here is how to do it:
const Zinky = require('zinky');
const basicauth = require('basicauth-middleware');
var app = new Zinky({
aliases: {
'customers': 'users'
}
});
app.addHook(basicauth('myname', 'somesecretword'));
app.listen();
Install the newly used package:
sudo npm install -g basicauth-middleware
Now run the app and open it in your browser.
Summarizing all that, gives: