Skip to content

Routing solution integrated Dependancy Injection for Express app

License

Notifications You must be signed in to change notification settings

tiengtinh/diap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Diap

Dependancy Injection with routing support for ExpressJs project.

install diap

Example Usage

server.coffee

diap          = require('diap')

diap.setup(
	app: app
	scanFolders: [fs.realpathSync('./server/app')]
	routes: require('./routes')
	classPostfixs: [ #filename with these postfix would be Class Type (autowired with new OuserService()). Others are value type
		'_controller'
		'_service'
	]
	globalMiddlewares: 
		whenNot: 
			public: (res, req, next) ->
				console.log 'whenNot middleware'
				next()
		#when:
)

routes.coffee

routes = [	
	{
		path: '/api/user',
		method: 'GET',
		run: [ 
			'apiController.users'
		]
	}
	{
		path: '/api/test',
		method: 'GET',
		run: [ 
			'apiController.test'
		]
	}
	{
		path: '/partial/:name',
		method: 'GET',
		run: [ 
			(req, res, next) ->
				console.log 'before partial'
				next()
			(req, res) ->
				res.render('partials/' + req.params.name)
		]
		public: true
	}
	{
		path: '*',
		method: 'GET',
		run: [ 
			'serverController.layout'
		]
		public: true
	}
]

module.exports = routes

api_controller.coffee

class ApiController
	constructor: (@ouserService) ->

	users: (req, res) =>   
		@ouserService.list().then (users) ->
			res.json users

	test: (req, res) =>   		
		res.json 'test'

module.exports = ApiController

About

Routing solution integrated Dependancy Injection for Express app

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CoffeeScript 98.6%
  • JavaScript 1.2%
  • Shell 0.2%