Permalink
Browse files

Reformatting files...

  • Loading branch information...
1 parent 807272f commit 6ae068709bcf9d885663fe66655b066e5b081806 xFontroSF committed Jan 18, 2013
Showing with 76 additions and 125 deletions.
  1. +64 −49 app.js
  2. +0 −62 client.js
  3. +10 −10 operacionsAritmetiques.js
  4. +2 −3 package.json
  5. +0 −1 post.txt
View
@@ -1,83 +1,98 @@
// Express sample aritmetic application.
-var express = require("express"),
- operacionsAritmetiques = require("./operacionsAritmetiques");
+var express = require('express');
+var operacionsAritmetiques = require('./operacionsAritmetiques');
+var args = require('commander');
-var port = process.env.OPENSHIFT_INTERNAL_PORT || process.env.VMC_APP_PORT || "8080",
- app = express(),
- handle = {};
+var app = express();
+var handle = {};
-app.configure(function (){
+args
+ .version(0.1)
+ .option('-p, --port [number]', 'Auth Server Port', 3000)
+ .parse(process.argv);
+
+var port = args.port;
+
+app.configure(function () {
app.use(express.bodyParser());
});
// Handlers for POST.
-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("/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("/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("/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.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 });
+ }
+ });
+});
// Handlers for GET
-app.get("/:operation", function (request, response, next){
+app.get("/:operation", function (request, response, next) {
var op;
- if(request.params.operation == "sumar"){
+ if (request.params.operation == "sumar") {
op = operacionsAritmetiques.sumar;
- }else if(request.params.operation == "restar"){
+ } else if(request.params.operation == "restar") {
op = operacionsAritmetiques.restar;
- }else if(request.params.operation == "multiplicar"){
+ } else if(request.params.operation == "multiplicar") {
op = operacionsAritmetiques.multiplicar;
- }else if(request.params.operation == "dividir"){
+ } else if(request.params.operation == "dividir") {
op = operacionsAritmetiques.dividir;
- }else{
+ } else {
op = undefined;
}
- if(typeof op === 'function'){
+ if (typeof op === 'function') {
op( parseInt(request.param('op1')),
parseInt(request.param('op2')),
- function(error, resultat){
- if(error){
+ function (error, resultat) {
+ if (error) {
response.json(error.errorCode, { error: error.errorDescription });
- }else {
- response.json(200, {resultat: resultat});
+ } else {
+ response.json(200, { resultat: resultat });
}
});
- }else {
+ } else {
next();
}
});
// Default handler
-app.all("/*", function (request, response){
- response.json(404, {error:'404 Not Found'});
+app.all("/*", function (request, response) {
+ response.json(404, { error:'404 Not Found' });
});
app.listen(port);
-console.log("Server has started at port: "+port);
+console.log("Server has started at port: " + port);
View
@@ -1,62 +0,0 @@
-var http = require("http");
-
-var req_options = {
- host: '192.168.7.13',
- //host: 'localhost',
- port: '8080',
- method: 'POST',
- //path: '/sumar?op1=2&op2=4', //Exemple per a GET.
- path: 'http://aritmeticops-nodetestingsf.rhcloud.com/sumar', //Exemple per a POST.
- //path: '/surar',
- headers: {
- Host: "aritmeticops-nodetestingsf.rhcloud.com"
- }
- };
-
-
-var nums = {
- op1: 7,
- op2: 16
-};
-
-var data = JSON.stringify(nums);
-
-
-// Request que fa el client.
-if(req_options.method === 'POST'){
- req_options.headers['Content-Length'] = data.length;
- req_options.headers['Content-Type'] = 'application/json';
-
- var req = http.request(req_options, function(response){
- var res_data='';
- response.on('data', function(chunk){
- res_data +=chunk;
- });
- response.on('end', function(){
- console.log("El resultat es: " + JSON.parse(res_data).resultat);
- console.log("JSON: "+res_data);
- });
- });
-
- // Tractament d'errors.
- req.on('error', function(e) {
- console.log("Got error: " + e.message);
- });
-
- req.write(data);
- //console.log("The data i pass: "+data);
- req.end();
-
-} else if(req_options.method === 'GET'){
- var req = http.get(req_options, function(response){
- var res_data='';
- response.on('data', function(chunk){
- res_data +=chunk;
- });
- response.on('end', function(){
- console.log("El resultat es: " + JSON.parse(res_data).resultat);
- });
- });
-}
-
-
@@ -1,32 +1,32 @@
var sumar = function (a, b, res){
- var result = a+b;
+ var result = a + b;
checkNumbersAndRespond(result, res);
};
var restar = function (a, b, res) {
- var result = a-b;
+ var result = a - b;
checkNumbersAndRespond(result, res);
};
var multiplicar = function (a, b, res) {
- var result = a*b;
+ var result = a * b;
checkNumbersAndRespond(result, res);
};
-var dividir = function (a, b, res){
- var result = a/b;
+var dividir = function (a, b, res) {
+ var result = a / b;
checkNumbersAndRespond(result, res);
};
-var arrel = function (a, b, res){
+var arrel = function (a, b, res) {
var result = 0;
- for(var i=0;i<1000;i++){
+ for (var i = 0;i < 1000;i++) {
result += sqrt(a) * sqrt(b);
}
};
-var checkNumbersAndRespond = function (result, res){
- if(isNaN(result) || result === undefined){
+var checkNumbersAndRespond = function (result, res) {
+ if (isNaN(result) || result === undefined) {
var error = {
errorCode: 400,
errorContent: {
@@ -35,7 +35,7 @@ var checkNumbersAndRespond = function (result, res){
errorDescription: '400 Bad Request'
};
res(error);
- } else{
+ } else {
res(null, result);
}
};
View
@@ -5,7 +5,6 @@
"keywords": [
"Node.js",
"application",
- "openshift",
"aritmetic"
],
"author": {
@@ -17,9 +16,9 @@
"node": ">= 0.6.0",
"npm": ">= 1.0.0"
},
-
"dependencies": {
- "express": ">= 3.0.0rc4"
+ "express": ">= 3.0.0rc4",
+ "commander": "*"
},
"devDependencies": {},
"bundleDependencies": [],
View
@@ -1 +0,0 @@
-{"op1":100,"op2":200}

0 comments on commit 6ae0687

Please sign in to comment.