5
5
var express = require ( 'express' ) ;
6
6
var paginate = require ( 'express-paginate' ) ;
7
7
var Route = require ( 'route-parser' ) ;
8
- var define = require ( 'define-js' ) ;
9
8
var unless = require ( 'express-unless' ) ;
9
+ var vhost = require ( 'vhost' ) ;
10
+ var define = require ( 'define-js' ) ;
10
11
var parse = require ( 'parseparams' ) ;
11
12
var { BusinessBehaviourType, BusinessBehaviour } = require ( 'behaviours-js' ) ;
12
13
var businessController = require ( './controller.js' ) . businessController ;
@@ -49,6 +50,8 @@ var behaviours = {
49
50
50
51
var BEHAVIOURS = { } ;
51
52
53
+ var defaultRemotes = { } ;
54
+
52
55
var FetchBehaviours = { } ;
53
56
54
57
var LogBehaviours = { } ;
@@ -77,8 +80,6 @@ var types = {
77
80
78
81
var defaultPrefix = '/' ;
79
82
80
- var defaultRemotes = { } ;
81
-
82
83
backend . behaviour = function ( path , config ) {
83
84
84
85
if ( typeof app !== 'function' || typeof app . use !== 'function' ) {
@@ -164,8 +165,14 @@ backend.behaviour = function (path, config) {
164
165
if ( typeof plugin === 'function' && parse ( plugin ) [ 0 ] === 'out' ) return plugin ;
165
166
return res_plugin ;
166
167
} , undefined ) ;
167
- var prefix = typeof path === 'string' && path . length > 0 ?
168
- join ( defaultPrefix , path ) : defaultPrefix !== '/' ? defaultPrefix : null ;
168
+ var prefix ;
169
+ var overwritePath ;
170
+ if ( typeof config === 'object' ) overwritePath = config . overwritePath ;
171
+ if ( typeof path === 'string' && path . length > 0 ) {
172
+
173
+ if ( overwritePath ) prefix = path ;
174
+ else prefix = join ( defaultPrefix , path ) ;
175
+ } else if ( defaultPrefix !== '/' && ! overwritePath ) prefix = defaultPrefix ;
169
176
var behaviour_runner = function ( req , res , next , inputObjects , er ) {
170
177
171
178
var signature = getSignature ( req ) ;
@@ -210,7 +217,7 @@ backend.behaviour = function (path, config) {
210
217
request . next ( error || er || new Error ( 'Error while executing ' +
211
218
options . name + ' behaviour, version ' + options . version + '!' ) ) ;
212
219
} else if ( ! res_plugin ||
213
- res_plugin ( behaviourResponse , request . req , request . res , request . next ) ) {
220
+ ! res_plugin ( behaviourResponse , request . req , request . res , request . next ) ) {
214
221
215
222
response . response = options . paginate ? behaviourResponse . modelObjects ||
216
223
behaviourResponse : behaviourResponse ;
@@ -311,6 +318,11 @@ backend.behaviour = function (path, config) {
311
318
}
312
319
} ) ;
313
320
}
321
+ if ( typeof options . host === 'string' && options . host . length > 0 ) {
322
+
323
+ req_handler = vhost ( options . host , req_handler ) ;
324
+ if ( req_plugin ) req_plugin = vhost ( options . host , req_plugin ) ;
325
+ }
314
326
if ( isRoute ) {
315
327
316
328
var keys = Object . keys ( behaviours ) ;
@@ -351,6 +363,7 @@ backend.behaviour = function (path, config) {
351
363
version : options . version ,
352
364
method : options . method ,
353
365
path : options . path ,
366
+ prefix : prefix ,
354
367
origins : options . origins ,
355
368
credentials : options . credentials ,
356
369
maxAge : options . maxAge ,
@@ -387,4 +400,4 @@ backend.behaviours = function (path, parser, remotes) {
387
400
return behaviours ;
388
401
} ;
389
402
390
- backend . routes = behaviours ;
403
+ backend . routes = behaviours ;
0 commit comments