Skip to content

Instantly share code, notes, and snippets.

@umeboshi2 umeboshi2/server.js
Created Jul 30, 2016

Embed
What would you like to do?
Node.js Sample Server with Restify
/*
* Node.js Sample Server with Restify
* Copyright (C) 2014 - Thiago Uriel M. Garcia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var cluster = require('cluster');
if (cluster.isMaster)
{
console.log('Server is active. Forking workers now.');
var cpuCount = require('os').cpus().length;
for (var i=0; i<cpuCount; i++)
{
cluster.fork();
}
cluster.on('exit', function(worker)
{
console.error('Worker %s has died! Creating a new one.', worker.id);
cluster.fork();
});
}
else
{
var restify = require('restify')
, server = restify.createServer({'Node.js Sample Server', '1.0.0'})
, port = (process.env.PORT || 8088)
, routes = require('./app/routes.js');
server.use(restify.authorizationParser());
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.listen(port, function()
{
routes.initialize(server);
console.log('Worker %s spawned for port %s.', cluster.worker.id, port);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.