File tree Expand file tree Collapse file tree 6 files changed +113
-0
lines changed Expand file tree Collapse file tree 6 files changed +113
-0
lines changed Original file line number Diff line number Diff line change 1+ const cls = require ( 'cls-hooked' ) ;
2+ const ns = cls . createNamespace ( "Request-Context-5195409c4e8d71bf5dd408342f5942bb" ) ;
3+
4+ class Util {
5+ static middleware ( req , res , next ) {
6+ ns . run ( ( ) => next ( ) ) ;
7+ }
8+ static get ( key ) {
9+ if ( ns && ns . active ) {
10+ return ns . get ( key ) ;
11+ }
12+ }
13+ static set ( key , value ) {
14+ if ( ns && ns . active ) {
15+ return ns . set ( key , value ) ;
16+ }
17+ }
18+ }
19+ module . exports = Util ;
Original file line number Diff line number Diff line change 1+ const express = require ( 'express' ) ;
2+ const HttpContext = require ( 'node-http-context' ) ;
3+ const app = express ( ) ;
4+ const util = require ( './util' ) ;
5+
6+ HttpContext . Initilize ( app ) ;
7+
8+ app . use ( '/test' , ( req , res , next ) => {
9+ HttpContext . Response . json ( { success : true } ) ;
10+ } ) ;
11+
12+ app . use ( '/test2' , ( req , res , next ) => {
13+ util . someAction ( ) ;
14+ } ) ;
15+
16+ app . listen ( 5000 , ( ) => {
17+ console . log ( "Server started" ) ;
18+ } ) ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " node-http-context-example" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " Node Http Context example" ,
5+ "main" : " indexjs " ,
6+ "scripts" : {
7+ "start" : " node index.js"
8+ },
9+ "author" : " Devesh" ,
10+ "license" : " ISC" ,
11+ "dependencies" : {
12+ "express" : " ^4.17.1" ,
13+ "node-http-context" : " ^1.0.0"
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ const HttpContext = require ( 'node-http-context' ) ;
2+
3+ module . exports = {
4+ someAction : ( ) => {
5+ HttpContext . Response . send ( "Welcome" ) ;
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ const Util = require ( './Util' ) ;
2+
3+ class HttpContext {
4+ /**
5+ * @param {Object } app - Express application instance.
6+ */
7+ static Initilize ( app ) {
8+ if ( app ) {
9+ app . use ( Util . middleware ) ;
10+ app . use ( ( req , res , next ) => {
11+ Util . set ( 'Request' , req ) ;
12+ Util . set ( 'Response' , res ) ;
13+ Util . set ( 'Next' , next ) ;
14+ Util . set ( 'Session' , req . session || null ) ;
15+ next ( ) ;
16+ } ) ;
17+ } else {
18+ throw new Error ( "express not define" ) ;
19+ }
20+ }
21+
22+ static get Request ( ) { return Util . get ( 'Request' ) }
23+ static get Response ( ) { return Util . get ( 'Response' ) }
24+ static get Next ( ) { return Util . get ( 'Next' ) }
25+ static get Session ( ) { return Util . get ( 'Session' ) }
26+ }
27+ module . exports = HttpContext ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " node-http-context" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " Get HttpContext from anywhere" ,
5+ "main" : " index.js" ,
6+ "repository" : {
7+ "type" : " git" ,
8+ "url" : " https://github.com/dkrockcom/node-http-context.git"
9+ },
10+ "keywords" : [
11+ " HttpContext" ,
12+ " Request" ,
13+ " Response" ,
14+ " Express" ,
15+ " Http" ,
16+ " Nodejs"
17+ ],
18+ "author" : " Devesh Kumar" ,
19+ "license" : " MIT" ,
20+ "bugs" : {
21+ "url" : " https://github.com/dkrockcom/node-http-context/issues"
22+ },
23+ "homepage" : " https://github.com/dkrockcom/node-http-context" ,
24+ "dependencies" : {
25+ "cls-hooked" : " ^4.2.2"
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments