Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

V 1.1 Express + Cluster

  • Loading branch information...
commit 99cfb3c3845ca6a213a1236993dfc142eef1bb1f 1 parent c6611d6
xFontroSF authored
Showing with 84 additions and 49 deletions.
  1. +3 −1 package.json
  2. +81 −48 server.js
View
4 package.json
@@ -20,7 +20,9 @@
},
"dependencies": {
- "express": ">= 3.0.0rc4"
+ "express": ">= 3.0.0rc4",
+ "commander": ">= 1.0.4",
+ "cluster": ">= 0.7.7"
},
"devDependencies": {},
"bundleDependencies": [],
View
129 server.js
@@ -1,64 +1,97 @@
// Express sample aritmetic application.
var url = require("url"),
express = require("express"),
- operacionsAritmetiques = require("./operacionsAritmetiques");
+ operacionsAritmetiques = require("./operacionsAritmetiques"),
+ args = require("commander"),
+ cluster = require("cluster");
-var ipaddr = process.env.OPENSHIFT_INTERNAL_IP || "127.0.0.1",
+var ipaddr = process.env.OPENSHIFT_INTERNAL_IP || "127.0.0.1", //"192.168.7.171",
port = process.env.OPENSHIFT_INTERNAL_PORT || "8080";
var app = express(),
handle = {};
-handle["sumar"] = operacionsAritmetiques.sumar;
-handle["restar"] = operacionsAritmetiques.restar;
-handle["multiplicar"] = operacionsAritmetiques.multiplicar;
-handle["dividir"] = operacionsAritmetiques.dividir;
-handle["expensive"] = operacionsAritmetiques.arrel;
+args
+ .version('0.1')
+ .option('-p, --port [number]', 'client port', 8080)
+ .option('-f, --folder [folderpath]', 'root folder path', './' )
+ .option('-c, --cluster', 'cluster of processes')
+ .parse(process.argv);
-app.configure(function (){
- app.use(express.bodyParser());
-});
+if(cluster.isMaster) {
+ console.log('Binding to port ', args.port);
+ console.log('Folder path: ', args.folder);
+ console.log('Processor: ' + require('os').cpus()[0].model);
-app.post("/:operation", function(request, response, next){
- var op = handle[request.params.operation];
-
- if(typeof op === 'function'){
- op( request.body.op1,
- request.body.op2,
- function(error, resultat){
- if(error){
- response.json(error.errorCode, { error: error.errorDescription });
- }else {
- response.json(200, {resultat: resultat});
- }
- });
- }else{
- next();
- }
-});
-
-app.get("/:operation", function (request, response, next){
- var op = handle[request.params.operation];
-
- if(typeof op === 'function'){
- op( parseInt(request.param('op1')),
- parseInt(request.param('op2')),
- function(error, resultat){
- if(error){
- response.json(error.errorCode, { error: error.errorDescription });
- }else {
- response.json(200, {resultat: resultat});
- }
+ if(args.cluster) {
+ console.log('Cluster: ' + args.cluster);
+ require('os').cpus().forEach(function (item) {
+ cluster.fork();
});
- }else {
- next();
+ } else {
+ console.log('Cluster: no cluster');
+ cluster.fork();
}
-});
+} else {
+
+ app.configure(function (){
+ app.use(express.bodyParser());
+ });
+
+ app.post("/sumar", function(request, response){operacionsAritmetiques.sumar(request.body.op1, request.body.op2, function(error, resultat){
+ if(error){
+ response.json(error.errorCode, { error: error.errorDescription });
+ }else {
+ response.json(200, {resultat: resultat});
+ }
+ })});
+
+ app.post("/restar", function(request, response){operacionsAritmetiques.restar(request.body.op1, request.body.op2, function(error, resultat){
+ if(error){
+ response.json(error.errorCode, { error: error.errorDescription });
+ }else {
+ response.json(200, {resultat: resultat});
+ }
+ })});
+
+ app.post("/multiplicar", function(request, response){operacionsAritmetiques.multiplicar(request.body.op1, request.body.op2, function(error, resultat){
+ if(error){
+ response.json(error.errorCode, { error: error.errorDescription });
+ }else {
+ response.json(200, {resultat: resultat});
+ }
+ })});
+
+ app.post("/dividir", function(request, response){operacionsAritmetiques.dividir(request.body.op1, request.body.op2, function(error, resultat){
+ if(error){
+ response.json(error.errorCode, { error: error.errorDescription });
+ }else {
+ response.json(200, {resultat: resultat});
+ }
+ })});
-app.all("/*", function (request, response){
- response.json(404, {error:'404 Not Found'});
-});
+ app.get("/:operation", function (request, response, next){
+ var op = handle[request.params.operation];
+
+ if(typeof op === 'function'){
+ op( parseInt(request.param('op1')),
+ parseInt(request.param('op2')),
+ function(error, resultat){
+ if(error){
+ response.json(error.errorCode, { error: error.errorDescription });
+ }else {
+ response.json(200, {resultat: resultat});
+ }
+ });
+ }else {
+ next();
+ }
+ });
-app.listen(port,ipaddr);
-console.log("Server has started at: "+ipaddr+":"+port);
+ app.all("/*", function (request, response){
+ response.json(404, {error:'404 Not Found'});
+ });
+ app.listen(args.port,ipaddr);
+ console.log("Server has started at: "+ipaddr+":"+port);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.