├── server.js ├── README └── Application.js /server.js: -------------------------------------------------------------------------------- 1 | 2 | // Start the ColdFusion server. 3 | require( "coldfusion" ).start(); -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ColdFusion.js 2 | 3 | This project is an experiment in moving some of the ColdFusion Application Framework (Application.cfc) concepts into a Node.js context. This includes centralized hooks into the various events of the ColdFusion.js lifecycle: 4 | 5 | onApplicationStart() => callback( true | false ) 6 | onSessionStart() 7 | onRequestStart() => callback( true | false ) 8 | onRequest() 9 | onRequestEnd() 10 | onSessionEnd() 11 | onApplicationEnd() 12 | onError() => true | false 13 | 14 | These event handlers are defined in the Application.js file - a root-level module that exports these methods in a single constructor. None of the methods are requires. They are only used if they are present. 15 | 16 | Like the ColdFusion application framework, session management can be managed on a per-request basis. Cookies can be overriden so that session cookies can be encrypted. 17 | -------------------------------------------------------------------------------- /Application.js: -------------------------------------------------------------------------------- 1 | 2 | // Define the factory object for Application.js. Since each incoming 3 | // request has to define a new instance of the Application.js, we 4 | // have to get around the module-based caching. This gives every 5 | // request the opportunity to re-define the settings (this is a good 6 | // thing - and a very powerful thing). 7 | module.exports = function( request, response ){ 8 | 9 | 10 | // Define the application settings. 11 | this.name = "ColdFusion.js Test App"; 12 | 13 | // I am the amount of time (in seconds) the application can 14 | // sit idle before it timesout. Once timing out, the 15 | // onApplicationEnd() event handler will be implicitly invoked, 16 | // and any subsequent request will boot up a new application. 17 | this.applicationTimeout = 15; 18 | 19 | // I determine whether or not session management is being used 20 | // for this page request. This can be turned on or off for each 21 | // request. 22 | this.sessionManagement = true; 23 | 24 | // I am the amount of time (in seconds) the session can sit idle 25 | // before it timesout. Once timing out, the onSessionEnd() event 26 | // handler is implicitly invoked, and any subsequent request will 27 | // boot up a new session. 28 | this.sessionTimeout = 5; 29 | 30 | // I determine if the session cookies should be set 31 | // automatically by the framework. If true, a sessionID cookie 32 | // will be sent back to the browser. If false, the cookie needs 33 | // to be manually set. 34 | this.setClientCookies = true; 35 | 36 | // I am the amount of time the request can run before it is 37 | // forced to end (optional - exclude this property for an open- 38 | // ended response time). 39 | this.requestTimeout = 5; 40 | 41 | 42 | // ------------------------------------------------------ // 43 | // ------------------------------------------------------ // 44 | 45 | 46 | // I initialize the application. 47 | this.onApplicationStart = function( request, response, callback ){ 48 | 49 | // Log event handler. 50 | console.log( ">> Event :: onApplicationStart()" ); 51 | console.log( ">>>> Application Name :: " + request.application.getName() ); 52 | 53 | // Set an application value. 54 | request.application.set( "message", "ColdFusion FTW!!" ); 55 | 56 | // Return true so the rest of the application can load. 57 | return( callback( true ) ); 58 | 59 | }; 60 | 61 | 62 | // I initialize the session. 63 | this.onSessionStart = function( request, response, callback ){ 64 | 65 | // Log event handler. 66 | console.log( ">> Event :: onSessionStart()" ); 67 | console.log( ">>>> Session ID :: " + request.session.getSessionID() ); 68 | 69 | // Store a session value. 70 | request.session.set( "hitCount", 0 ); 71 | 72 | // Return out so the framework knows the event is over. 73 | return( callback() ); 74 | 75 | }; 76 | 77 | 78 | // I initialize the request. 79 | this.onRequestStart = function( request, response, callback ){ 80 | 81 | // Log event handler. 82 | console.log( ">> Event :: onRequestStart()" ); 83 | 84 | // Increment the session-based hit-count. Notice that once a 85 | // value is stored in the session cache, it can be referenced 86 | // without a getter / setter. 87 | request.session.hitCount++; 88 | 89 | // Return true so the rest of the request can load. 90 | return( callback( true ) ); 91 | 92 | }; 93 | 94 | 95 | // I process the request. 96 | this.onRequest = function( request, response ){ 97 | 98 | // Log event handler. 99 | console.log( ">> Event :: onRequest()" ); 100 | 101 | // Set the content type. 102 | response.setHeader( "content-type", "text/html" ); 103 | 104 | // Write out some content. 105 | response.write( 106 | "

ColdFusion.js On Node.js

" + 107 | "

Hello - this is page request " + 108 | request.session.hitCount + 109 | ".

" 110 | ); 111 | 112 | // End the response. 113 | response.end( 114 | "

" + 115 | request.application.message + 116 | "

" 117 | ); 118 | 119 | }; 120 | 121 | 122 | // I tear down the request. 123 | this.onRequestEnd = function( request, response ){ 124 | 125 | // Log event handler. 126 | console.log( ">> Event :: onRequestEnd()" ); 127 | 128 | }; 129 | 130 | 131 | // I tear down the session. 132 | this.onSessionEnd = function( applicationScope, sessionScope ){ 133 | 134 | // Log event handler. 135 | console.log( ">> Event :: onSessionEnd()" ); 136 | console.log( ">>>> Session ID :: " + sessionScope.getSessionID() ); 137 | 138 | }; 139 | 140 | 141 | // I tear down the application. 142 | this.onApplicationEnd = function( applicationScope ){ 143 | 144 | // Log event handler. 145 | console.log( ">> Event :: onApplicationEnd()" ); 146 | console.log( ">>>> Application Name :: " + applicationScope.getName() ); 147 | 148 | }; 149 | 150 | 151 | // I handle any global errors. 152 | // 153 | // NOTE: The request / response objects may NOT be present. If 154 | // the error occurred during an asynchronous callback, this 155 | // Application.js instance might not even be the one that started 156 | // the action that ended up raising the exception. 157 | this.onError = function( error, request, response ){ 158 | 159 | // Log event handler. 160 | console.log( ">> Event :: onError()" ); 161 | console.log( ">>>> Error Message :: " + error.message ); 162 | 163 | // Check to see if there is a response associated with this 164 | // error is available and is not committed. 165 | if ( 166 | response && 167 | !response.isCommitted() 168 | ){ 169 | 170 | // Output the error. 171 | response.write( "

An error has occurred: " + error.message + ".

" ); 172 | 173 | } 174 | 175 | }; 176 | 177 | 178 | }; 179 | 180 | 181 | 182 | --------------------------------------------------------------------------------