Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Operacions Aritmetiques with Express

  • Loading branch information...
commit 3346f2f74a77783fa0a4555be7de36c6fe5416cc 1 parent c0a7fe0
xFontroSF authored
Showing with 52 additions and 75 deletions.
  1. +7 −5 client.js
  2. +1 −1  operacionsAritmetiques.js
  3. +1 −3 router.js
  4. +43 −66 server.js
View
12 client.js
@@ -2,11 +2,12 @@ var http = require("http");
var req_options = {
host: '192.168.7.13',
- //host: '127.0.0.1',
+ //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: 'http://aritmeticops-nodetestingsf.rhcloud.com/dividir', //Exemple per a POST.
+ //path: '/dividir',
headers: {
Host: "aritmeticops-nodetestingsf.rhcloud.com"
}
@@ -14,8 +15,8 @@ var req_options = {
var nums = {
- op1: 7,
- op2: 15
+ op1: 4,
+ op2: 16
};
var data = JSON.stringify(nums);
@@ -28,12 +29,12 @@ if(req_options.method === 'POST'){
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);
});
});
@@ -43,6 +44,7 @@ if(req_options.method === 'POST'){
});
req.write(data);
+ //console.log("The data i pass: "+data);
req.end();
} else if(req_options.method === 'GET'){
View
2  operacionsAritmetiques.js
@@ -19,7 +19,7 @@ var dividir = function (a, b, res){
};
var checkNumbersAndRespond = function (result, res){
- if(isNaN(result)){
+ if(isNaN(result) || result === undefined){
var error = {
errorCode: 400,
errorContent: {
View
4 router.js
@@ -1,6 +1,5 @@
function route(handle, operation, a, b, resultat) {
if (typeof handle[operation] === 'function') {
-
handle[operation](
a,
b,
@@ -8,8 +7,7 @@ function route(handle, operation, a, b, resultat) {
if(error){
resultat(error);
} else{
- //console.log("The result is: "+res);
- resultat(null ,{"resultat" : res});
+ resultat(null, res);
}
}
);
View
109 server.js
@@ -1,77 +1,54 @@
-// OpenShift sample Node application
-var http = require("http"),
- url = require("url"),
- qs = require("querystring");
+// Express sample aritmetic application.
+var url = require("url"),
+ express = require("express");
var ipaddr = process.env.OPENSHIFT_INTERNAL_IP || "127.0.0.1",
port = process.env.OPENSHIFT_INTERNAL_PORT || "8080";
function start(route, handle) {
- function onRequest(request, response) {
- var data = '',
- pathname = url.parse(request.url).pathname;
+ var app = express();
+ app.use(express.bodyParser());
+
+ app.post('/*', function(request, response){
+ var pathname = request.path;
- if(request.method === 'POST'){
- request.on('data', function(chunk){
- data += chunk;
- });
-
- request.on('end',
- function(){
- var dataObj = JSON.parse(data),
- op1 = parseInt(dataObj.op1),
- op2 = parseInt(dataObj.op2);
- //console.log("Request for " + pathname + " received. With operands "+op1+" and "+op2);
-
- route(
- handle,
- pathname,
- op1,
- op2,
- function(error, resultat){
- if(error){
- response.writeHead(error.errorCode, error.errorContent);
- response.write(JSON.stringify(error.errorDescription));
- response.end();
- }else {
- response.writeHead(200, {});
- response.write(JSON.stringify(resultat));
- response.end();
- }
- });
- });
-
- } else if(request.method === 'GET'){
- var parsedUrl = url.parse(request.url);
-
- var op1 = parseInt(qs.parse(parsedUrl.query).op1),
- op2 = parseInt(qs.parse(parsedUrl.query).op2);
- //console.log("Request for " + parsedUrl.pathname + " received.");
-
- route(
- handle,
- pathname,
- op1,
- op2,
- function(error, resultat){
- if(error){
- response.writeHead(error.errorCode, error.errorContent);
- response.write(JSON.stringify(error.errorDescription));
- response.end();
- }else {
- response.writeHead(200, {});
- response.write(JSON.stringify(resultat));
- response.end();
- }
- });
- }
-
- }
+ route(
+ handle,
+ pathname,
+ 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.get('/*', function(request,response){
+ var pathname = request.path;
+
+ route(
+ handle,
+ pathname,
+ 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});
+ }
+ });
+ });
+
+ app.listen(port,ipaddr);
- http.createServer(onRequest).listen(port,ipaddr);
- //console.log("Server has started at: "+ipaddr+":"+port);
+ console.log("Server has started at: "+ipaddr+":"+port);
}
-exports.start = start;
+exports.start = start;
+
Please sign in to comment.
Something went wrong with that request. Please try again.