CerberusMVC Documentation

Handler


The handler file called handler.js is the bridge, it has the initial callback for your provider, along with instantiation of the stack and loading of middleware and services. It finally returns the reponse abck to the providers ecosystem.

handler.js (example handler for AWS)

'use strict';

const Application = require('cerberus-mvc/System/Application');
const CorsMiddleware = require('cerberus-mvc/Middleware/Cors');
const KnexMiddleware = require('cerberus-mvc/Middleware/Knex');
const KnexService = require('cerberus-mvc/Service/Knex');

exports.run = (event, context, callback) => { 
	const app = new Application('aws');
	const corsMiddleware = new CorsMiddleware(); 
	const knexMiddleware = new KnexMiddleware(); 
	const yourDBKnexService = new KnexService('postgres', '192.168.1.10', 5432, 'your_db', 'your_user', 'your_password'); 
	
	app.service(yourDBKnexService);
	app.middleware(corsMiddleware);
	app.middleware(knexMiddleware);
	
	app.run(event).then((response) => callback(null, response))
};

handler.js (example handler for ExpressJS)

'use strict';

const os = require("os");
const cluster = require("cluster");
const clusterWorkerSize = os.cpus().length;

const Application = require('cerberus-mvc/System/Application');
const CorsMiddleware = require('cerberus-mvc/Middleware/Cors');
const KnexMiddleware = require('cerberus-mvc/Middleware/Knex');
const KnexService = require('cerberus-mvc/Service/Knex');

const express = require('express');
const requestIp = require('request-ip');
const bodyParser = require('body-parser');
const PORT = 8082;
const server = express();

if (clusterWorkerSize > 1) {
	if (cluster.isMaster) {
		for (let i = 0; i < clusterWorkerSize; i++) cluster.fork()
		cluster.on("exit", (worker) => console.log("Worker", worker.id, " has exitted."));
	} else process();
} else process();

function process() {
	server.use(requestIp.mw())
	server.use(bodyParser.urlencoded({ extended: false }))
	server.use(bodyParser.json())

	server.use('/', (req, res) => {
		let app = new Application('express');
		let corsMiddleware = new CorsMiddleware();
		let knexMiddleware = new KnexMiddleware();
		let yourDBKnexService = new KnexService('postgres', '192.168.1.10', 5432, 'your_db', 'your_user', 'your_password');

		app.service(yourDBKnexService);
		app.middleware(corsMiddleware);
		app.middleware(knexMiddleware);

		return app.run(req).then((response) => res.set(response.headers).status(response.status).send(response.body));
	});

	// Start
	server.listen(PORT, () => console.log('Running on http://localhost:' + PORT));
}

handler.js (example handler for Socket.IO)

'use strict';

const Application = require('cerberus-mvc/System/Application');
const express = require('express');
const http = require('http').Server(express());
const SOCKET_PORT = 3000
const io = require('socket.io')(http, {
	cors: {
		origin: "*",
		methods: ["GET", "POST"],
		allowedHeaders: ["authorization"]
	}
});

// const AuthService = require('./src/Service/Auth.js'); // add in your service
// const AuthMiddleware = require('./src/Middleware/Auth.js'); // add in your middleware

io.on('connection', (socket) => {
	socket.onAny((route, data) => {
		console.log(route, data);

		const app = new Application('socket');
		// const authService = new AuthService(); // add in your service
		// const authMiddleware = new AuthMiddleware(); // add in your middleware

		// app.service(authService); // add in your service
		// app.middleware(authMiddleware); // add in your middleware

		app.run({ route, data, socket, io });
	});
});

http.listen(SOCKET_PORT, () => {
	console.log('Socket API listening on http://localhost:' + SOCKET_PORT);
});

handler.js (combined express REST API and Socket.IO API)

Both API's share the same template file! Set teh method as 'socket' in your tmeplate file route.

'use strict';

const Application = require('cerberus-mvc/System/Application');
const CorsMiddleware = require('cerberus-mvc/Middleware/Cors');
const AuthMiddleware = require('./src/Middleware/Auth.js');

const express = require('express');
const requestIp = require('request-ip');
const bodyParser = require('body-parser');
const API_PORT = 8082;
const server = express();

server.use(requestIp.mw())
server.use(bodyParser.urlencoded({ extended: false }))
server.use(bodyParser.json())

server.use('/', (req, res) => {
	let app = new Application('express');
	let corsMiddleware = new CorsMiddleware();

	app.middleware(corsMiddleware);

	return app.run(req).then((response) => res.set(response.headers).status(response.status).send(response.body));
});

server.listen(API_PORT, () => console.log('Rest API listening on http://localhost:' + API_PORT));

const http = require('http').Server(express());
const SOCKET_PORT = 3000
const io = require('socket.io')(http, {
	cors: {
		origin: "*",
		methods: ["GET", "POST"],
		allowedHeaders: ["authorization"]
	}
});

io.on('connection', (socket) => {
	socket.onAny((route, data) => {		
		const app = new Application('socket');
		const authMiddleware = new AuthMiddleware();

		app.middleware(authMiddleware);

		app.run({ route, data, socket, io });
	});
});

http.listen(SOCKET_PORT, () => {
	console.log('Socket API listening on http://localhost:' + SOCKET_PORT);
});