Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Server working with Express!

  • Loading branch information...
commit 29ce7e14e58095abec9757427f093509fcf10586 1 parent 2188e2b
xFontroSF authored
View
2  client.js
@@ -7,7 +7,7 @@ var req_options = {
method: 'POST',
//path: '/sumar?op1=2&op2=4', //Exemple per a GET.
path: 'http://aritmeticops-nodetestingsf.rhcloud.com/dividir', //Exemple per a POST.
- //path: '/dividir',
+ //path: '/sumar',
headers: {
Host: "aritmeticops-nodetestingsf.rhcloud.com"
}
View
12 index.js
@@ -1,12 +0,0 @@
-var server = require("./server"),
- router = require("./router"),
- operacionsAritmetiques = require("./operacionsAritmetiques"),
- handle = {};
-
-handle["/sumar"] = operacionsAritmetiques.sumar;
-handle["/restar"] = operacionsAritmetiques.restar;
-handle["/multiplicar"] = operacionsAritmetiques.multiplicar;
-handle["/dividir"] = operacionsAritmetiques.dividir;
-handle["/expensive"] = operacionsAritmetiques.arrel;
-
-server.start(router.route, handle);
View
3  operacionsAritmetiques.js
@@ -43,4 +43,5 @@ var checkNumbersAndRespond = function (result, res){
exports.sumar = sumar;
exports.restar = restar;
exports.multiplicar = multiplicar;
-exports.dividir = dividir;
+exports.dividir = dividir;
+exports.arrel = arrel;
View
2  package.json
@@ -26,7 +26,7 @@
"bundleDependencies": [],
"scripts": {
- "start": "node ./index.js"
+ "start": "node ./server.js"
},
"private": true
View
26 router.js
@@ -1,26 +0,0 @@
-function route(handle, operation, a, b, resultat) {
- if (typeof handle[operation] === 'function') {
- handle[operation](
- a,
- b,
- function(error, res){
- if(error){
- resultat(error);
- } else{
- resultat(null, res);
- }
- }
- );
- } else {
- var error = {
- errorCode: 404,
- errorContent: {
- 'Content-Type' : 'text/html'
- },
- errorDescription: '404 Not foud'
- };
- resultat(error);
- }
-}
-
-exports.route = route;
View
86 server.js
@@ -1,53 +1,65 @@
// Express sample aritmetic application.
var url = require("url"),
- express = require("express");
-
+ express = require("express"),
+ operacionsAritmetiques = require("./operacionsAritmetiques");
var ipaddr = process.env.OPENSHIFT_INTERNAL_IP || "127.0.0.1",
port = process.env.OPENSHIFT_INTERNAL_PORT || "8080";
-function start(route, handle) {
- var app = express();
+var app = express(),
+ handle = {};
+
+handle["sumar"] = operacionsAritmetiques.sumar;
+handle["restar"] = operacionsAritmetiques.restar;
+handle["multiplicar"] = operacionsAritmetiques.multiplicar;
+handle["dividir"] = operacionsAritmetiques.dividir;
+handle["expensive"] = operacionsAritmetiques.arrel;
+
+app.configure(function (){
app.use(express.bodyParser());
- app.post('/*', function(request, response, next){
- var pathname = request.path;
-
- 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){
+});
+
+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});
}
});
- });
-
- app.listen(port,ipaddr);
-
- console.log("Server has started at: "+ipaddr+":"+port);
+ }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});
+ }
+ });
+ }else {
+ next();
+ }
+});
+
+app.all("/*", function (request, response){
+ response.json(404, {error:'404 Not Found'});
+});
+
+app.listen(port,ipaddr);
-exports.start = start;
+console.log("Server has started at: "+ipaddr+":"+port);
Please sign in to comment.
Something went wrong with that request. Please try again.