You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@raymondfeng here is the code scratch pad we used to pair program some of the basic implementation ideas.
varloopback=require('loopback');varboot=require('loopback-boot');varapp=module.exports=loopback();// Set up the /favicon.icoapp.use(loopback.favicon());// request pre-processing middlewareapp.use(loopback.compress());// -- Add your pre-processing middleware here --// boot scripts mount components like REST APIboot(app,__dirname);varroot=app.createPhase('root');foreach(phaseinconfig.phases){}foreach(phaseinapp.phases()){foreach(handlerinhandlersForGivenPhase){app.register(phase.name,hander)}root.add(phase);}// jugglerUser.create=function(data,cb){User.SOMETHING.execPhase('validate',function(){// the validate phase!// ... and so on...});}// hook into your methodPhase.wrapSync('yourMethod', ...);MyModel.register('beforeYourMethod',function(ctx){ctx.arguments})MyModel.yourMethod=functionproxy(){// executes concrete yourMethod// before phase// if sync// executes the after phase// async// wrap the callback// exec the after phase}Model.wrap=function(methodName,options){varmethodPhase=Model.createPhase(key);model[methodName]=function(){varargs=arguments;if(isSync){methodPhase.add('before',methodName,function(){originalMethod(args);methodPhase.add('after',methodName);});}else{varcb=getCb(args);methodPhase.add('before', ...,function(){methodPhase.add('after',methodName);});}}}app.register('foo:*',function(cb){console.log(ctx.phase.name);cb();});MyModel.register('afterYourMethod',function(ctx){ctx.arguments})app.models.User.register('validate',function(ctx,done){done(newError('nothing is valid!'));});// new imp for enable authapp.register('auth',function(ctx){// ...loopback.Contextctx.remotingContext.req});varexplorer;try{explorer=require('loopback-explorer');}catch(err){console.log('Run `npm install loopback-explorer` to enable the LoopBack explorer');return;}varrestApiRoot=server.get('restApiRoot');varexplorerApp=explorer(server,{basePath: restApiRoot});server.use('/explorer',explorerApp);server.once('started',function(){varbaseUrl=server.get('url').replace(/\/$/,'');// express 4.x (loopback 2.x) uses `mountpath`// express 3.x (loopback 1.x) uses `route`varexplorerPath=explorerApp.mountpath||explorerApp.route;console.log('Browse your REST API at %s%s',baseUrl,explorerPath);});varrestApiRoot=server.get('restApiRoot');server.use(restApiRoot,server.loopback.rest());// -- Mount static files here--// All static middleware should be registered at the end, as all requests// passing the static middleware are hitting the file system// Example:// app.use(loopback.static(path.resolve(__dirname', '../client')));varwebsitePath=require('path').resolve(__dirname,'../client');app.use(loopback.static(websitePath));// Requests that get this far won't be handled// by any middleware. Convert them into a 404 error// that will be handled later down the chain.app.use(loopback.urlNotFound());// The ultimate error handler.app.use(loopback.errorHandler());app.start=function(){// start the web serverreturnapp.listen(function(){app.emit('started');console.log('Web server listening at: %s',app.get('url'));});};// start the server if `$ node server.js`if(require.main===module){app.start();}
I have reviewed the code from the scratchpad. Few comments:
I find the names of the new methods little bit confusing, we should come up with a more descriptive name (e.g. onPhase(name, fn) instead of register(name, fn))
app.createPhase and root.add(phase) seem rather arbitrary to me, I don't understand what is the purpose. It's not a problem, I am just saying that I cannot asses this part.
The proposal does not handle phase ordering, nor Binding of handlers (global/name binding/dynamic binding)
Other than that, this looks like a sensible approach to me.
It would be great if we can build this incrementally in small steps. Start with something small yet complete, then add more stuff building on top of that. Perhaps start by replacing beforeValidate and afterValidate hooks with the new implementation?
I don't understand what is the relation of this feature with the middleware registration and ordering, if there is any relation. In particular, how will be phase.toMiddleware() mounted on the app?
Activity
raymondfeng commentedon Oct 7, 2014
Let's borrow some ideas from JAX-RS:
https://jersey.java.net/documentation/latest/filters-and-interceptors.html
In particular:
ritch commentedon Oct 7, 2014
@raymondfeng here is the code scratch pad we used to pair program some of the basic implementation ideas.
bajtos commentedon Oct 9, 2014
I have reviewed the code from the scratchpad. Few comments:
onPhase(name, fn)instead ofregister(name, fn))app.createPhaseandroot.add(phase)seem rather arbitrary to me, I don't understand what is the purpose. It's not a problem, I am just saying that I cannot asses this part.Other than that, this looks like a sensible approach to me.
It would be great if we can build this incrementally in small steps. Start with something small yet complete, then add more stuff building on top of that. Perhaps start by replacing
beforeValidateandafterValidatehooks with the new implementation?I don't understand what is the relation of this feature with the middleware registration and ordering, if there is any relation. In particular, how will be
phase.toMiddleware()mounted on the app?57 remaining items
dhmlau commentedon Aug 23, 2018
Since the last comment was almost 4 years ago, closing due to inactivity. Please create a new issue if you are still running into problems.