autobahnjs
deepjs/server oriented tools and middlewares for expressjs.
Autobahnjs comes with two kind of tools :
- middlewares for expressjs
- context : assign context to each incoming request
- modes : assign modes (ocm related) to each incoming request (based on current session)
- restful : map-to-service middleware (HTTP/rest dynamic routing)
- html : map-to-html middleware (deep-views/routes render map)
- statics : map-to-files middleware (dynamic map for statics files server)
- login : retrieve matched user and hold user in current session.
- logout : break current session.
- "App" object definition and tools for session/user management through deepjs chained API.
install
As autobahnjs is an environnement that tie a bunch of tools together and should be piloted from your own index.js : You should use the yeoman autobahnjs generator to get a workable structure.
coming really soon.
Or simply use the example folder, provided in autobahn lib under autobahnjs/lib. when you're in (after clone or npm install) :
npm install
node index.js
Enjoy.
Example of autobahnjs "app" and expressjs skeleton
var deep = express = autobahn = // bunch of middlware for expressjs Unit = ; // for deepjs unit testing var cookieParser = ;var session = ;var bodyParser = ; // bind actual JSON-Schema validator. taking one from deepjs core.deepValidator; // set root path and cwd (used in certain protocol/stores/chains)deepglobalsrootPath = __dirname;deep; // assign default modes.deep; //_________________________ Start your app constructionvar app = ; // simple app examplevar autobahnApp = autobahn; app// set simple session management (pure expressjs)// to get posted body parsed automatically (json/files/..)// ------ context and modes // create and bind unique context to each incoming request // assign OCM modes to each incoming req. context// ------ login and logout // catch post on /logout and break session. // catch post on /login and try to login// ------ Your maps-to-* middleware // deep-restful map-to-services // deep-views/deep-routes map-to-html rendering. // map-to-statics file server///____________________________________________________ Finish app construction; // bind global app. Allow us to apply login/logout/impersonate (and more) from chained API.deep; console; var repl = start prompt: "node via stdin> " input: processstdin output: processstdout;
Example of restful map
For full info on how managing/designing restful stores, see deep-restful and deepjs core documentation.
var deep = ;;moduleexports = "/user": deep "/foo":deep;
Example of html map
For full info on how managing/designing html routes map, see deep-views and deep-routes documentation.
var deep = ;; // create json client that look from /www. use it under "json" protocol. protocol: "json" basePath: "/www"; // create swigjs client that look from /www. use it under "swig" protocol. protocol: "swig" basePath: "/www";// map for html pages produced by server. the map itself, or the entries of the map, could be OCM.// example of map :moduleexports = head:deep index:deep ;
Example of statics map
var deep = ;// map for static files served by server (the map itself could be OCM)moduleexports = "/": // serve root path: deepglobalsrootPath + '/www' options: // native expressjs connect-statics options maxAge: 86400000 redirect: false ;
Usage from outside.
Outside means from you prefered http client (maybe your browser) to the server. If you use previous map in your autobahnjs/express skeleton (as above) :
Open your browser and try by example :
http://127.0.0.1:3000/
http://127.0.0.1:3000/hello
http://127.0.0.1:3000/hello/jhonny
http://127.0.0.1:3000/foo
http://127.0.0.1:3000/foo/e1
http://127.0.0.1:3000/foo/?bar
...
And with your prefered restful client, you should try to post/put/patch/del/get/range/... to /foo service.
Usage from "inside"
Inside means from nodejs CLI or from any script executed server side.
- deep.App(appObj) : holds appObj as main app accessible through chains.
- deep.login({ email:"john@doe.com", password:"test123"}) : start a contextualised chain, create session, retrieve user, etc. exactly as if you had really logged in from outside (i.e. through http client)
- .login(...) : from anywhere in a chain, do the same thing than deep.login
- deep.impersonate({ id:"..." }) : start a contextualised chain, rerieve user an do login without password.
- .impersonate(...) : from anywhere in a chain, do the same than deep.impersonate
- .logout() : : from anywhere in a chain, break current session
example :
deep;//...deep // endoss localy user identity (context, session, modes, ...) // take "myCollection" restful collection // put something in it as john doe. // break current chain session // endoss localy other user identity // ...;
deepjs core provides additionnaly the .modes(...)
API that allow you, by example, to do :
deep; // you should see an array with users.
Manage multiple app (advanced)
Imagine that you produce different expressjs skeleton/autobhan js app in the same nodejs process.
You should want to switch from one to another and/or execute each in its own context. For that you have two method that encapsulate the provided app in chain context.
- deep.app(appObj) : start a contextualised chain that hold appObj in its context. (advanced)
- .app(appObj) : from anywhere in a chain, hold appObj in its context. (advanced)