Permalink
Browse files

express example added

  • Loading branch information...
1 parent 6aaaf44 commit 9b15c41ddf89c2fbd228deae923ef9146045b06a @warmuuh committed Apr 3, 2013
@@ -5,11 +5,13 @@ function MyService() {
MyService.prototype = {
-
- handleGet: function (req, res, next)/*@GET("/")*/ {
- res.send('Look ma, no HTML!');
+ index: function (req, res, next)/*@GET*/ {
+ return 'index';
+ },
+
+ test: function (req, res, next)/*@POST*/ {
+ return {view:'test', model:req.body};
}
-
};
@@ -3,17 +3,30 @@
var wire = require("wire");
+var express= require("express");
+var yaap = require("../yaap/wire/express");
+
+/*
+var app = express();
+app.use(express.bodyParser());
+app.use("/index", function(req, res){res.render("index.jade");});
+app.use("/test", function(req, res){console.log(req.body);res.render("test.jade", req.body);});
+app.listen(8000);
+*/
+
wire({
- app: { create: 'express' , ready:{listen:[8000]}},
+ app: { create: 'express',
+ init:{use:[express.bodyParser()]},
+ ready:{listen:[8000]}
+ },
myService: { create: './MyService' },
plugins: [
- {module: "../yaap/wire"},
- {module: "../yaap/wire/express", server: "app"}
+ {module: "../yaap/wire"},
+ {module: "../yaap/wire/express",server: "app"}
]
}, {require: require}).then(function(ctx){
-
console.log("----- initialized -----");
//ctx.destroy();
}, function(err){console.error(err);});
@@ -0,0 +1,9 @@
+html
+ head
+ body
+ h1|Hello, what is your name?
+ div
+ form(name="input", action='/test',method='post')
+ label(for='name') Name
+ input(id='name', name="name",type='text',value='',placeholder='Type your name')
+ input(type='submit',value='Send!')
@@ -0,0 +1,7 @@
+html
+ head
+ body
+ h1|Hello
+ span=name
+
+
@@ -19,38 +19,44 @@
(function(define) {
define([ "when"],
function(when) {
+
+
+ function registerExpressCallback(path, fnExpress, obj, fnName, context){
+ path = path || '/' + fnName; //in case of no-argument annotation, use fnName as endpoint
+ fnExpress(path, function(req, res, next){
+ var result = obj[fnName](req, res, next); //dynamic call because there could be other annotations
+ if (typeof result === 'string')
+ res.render(result + '.' + context.express_view);
+ else if (result && result.view){
+ res.render(result.view + '.' + context.express_view, result.model);
+ }
+ });
+ };
+
return [
{
annotation: "@GET",
processFunction: function(obj, fnDescription, annotationParams, context){
- context.express.get(annotationParams[0], function(req, res, next){
- obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
- });
+ registerExpressCallback(annotationParams[0], context.express.get.bind(context.express), obj, fnDescription.name, context);
}
},
{
annotation: "@PUT",
- processFunction: function(obj, fnDescription, annotationParams, context){
- context.express.put(annotationParams[0], function(req, res, next){
- obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
- });
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ registerExpressCallback(annotationParams[0], context.express.put.bind(context.express), obj, fnDescription.name, context);
}
},
{
annotation: "@POST",
processFunction: function(obj, fnDescription, annotationParams, context){
- context.express.post(annotationParams[0], function(req, res, next){
- obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
- });
+ registerExpressCallback(annotationParams[0], context.express.post.bind(context.express), obj, fnDescription.name, context);
}
},
{
annotation: "@DELETE",
processFunction: function(obj, fnDescription, annotationParams, context){
- context.express["delete"](annotationParams[0], function(req, res, next){
- obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
- });
+ registerExpressCallback(annotationParams[0], context.express["delete"].bind(context.express), obj, fnDescription.name, context);
}
}
];
@@ -24,6 +24,7 @@
//register for wiring
yaap.config = yaap.config || {};
yaap.config.express = {$ref: options.server};
+ yaap.config.express_view = options.view || "jade";
for(var i = 0; i < VERBS.length; ++i)
yaap.register(VERBS[i]);
return {};
@@ -43,8 +43,8 @@ return {
var origFn = obj[fnDescription.name];
//console.log("@Autowire "+ JSON.stringify(resolvedRefs));
obj[fnDescription.name] = function(){
- for(var i = 0; i < annotatedParameters.length; ++i){
- var param = annotatedParameters[i];
+ for(var i = 0; i < annotatedParameters.length; ++i){
+ var param = annotatedParameters[i];
while (arguments.length -1 < param.index)
[].push.call(arguments, undefined);

0 comments on commit 9b15c41

Please sign in to comment.