Introduction to Kraken-JS
Kraken.js is a lightweight and powerful layer that extends the express framework by providing structure and conventions. It offers a wealth of features and middleware to help developers build scalable and maintainable Node.js applications.
Getting Started with Kraken-JS
To get started with Kraken.js, first install it via npm:
npm install kraken-js
Initialize your project with Kraken:
kraken init my-project
Configuration Management
Kraken.js utilizes a powerful configuration management system. Configuration files are placed in the config/
directory and are automatically loaded and merged based on the environment.
// config/development.json
{
"port": 8000,
"app": {
"title": "Development App"
}
}
Middleware
Kraken.js integrates deeply with Express middleware. You can add custom middleware or use built-in middleware:
var kraken = require('kraken-js');
var express = require('express');
var app = express();
app.use(kraken());
app.use(function (req, res, next) {
console.log('Custom Middleware');
next();
});
Router and Controllers
Kraken.js supports a controller-based routing system. Define your routes and controllers separately and map them in your main application file:
// controllers/home.js
exports.index = function(req, res) {
res.send('Home Page');
};
// routes/index.js
module.exports = function (router) {
var home = require('../controllers/home');
router.get('/', home.index);
};
// index.js
var app = require('express')();
var kraken = require('kraken-js');
app.use(kraken({
onconfig: function (config, next) {
next(null, config);
}
}));
app.listen(8000);
Security
Implemented security features within Kraken.js enhance the safety and protection of your application:
var lusca = require('lusca');
app.use(lusca.csrf());
app.use(lusca.xframe('SAMEORIGIN'));
app.use(lusca.hsts({ maxAge: 31536000 }));
Application Example
Here’s a complete example of a Kraken.js application using the aforementioned APIs:
var kraken = require('kraken-js');
var express = require('express');
var lusca = require('lusca');
var app = express();
app.use(kraken({
onconfig: function (config, next) {
next(null, config);
}
}));
app.use(lusca.csrf());
app.use(lusca.xframe('SAMEORIGIN'));
app.use(lusca.hsts({ maxAge: 31536000 }));
app.get('/', function (req, res) {
res.send('Welcome to Kraken.js App');
});
app.listen(8000, function () {
console.log('Listening on port 8000');
});
With Kraken.js, you can build robust and structured applications with greater ease and security. The conventions and tools provided make development more manageable and streamline the workflow.
Hash: 479e4e4d649ba4947f63c303b4cafa4d6f6aa5599a0212c6bddc8bfa24facb90