Created
September 22, 2017 15:25
-
-
Save garenyondem/c43cb83fd4e2b31eeff9e3f565f70aba to your computer and use it in GitHub Desktop.
Node.js Sample Server with Restify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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