Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
openui5_connect task: call "connect" middleware function if provided
Browse files Browse the repository at this point in the history
If a middleware function was provided as "connect" task option, it will
now be called after creating the middleware for the "openui5_connect"
task to allow injecting custom middleware or modifying the existing.
  • Loading branch information
matz3 committed Mar 16, 2015
1 parent aa16e59 commit 1cab971
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ module.exports = function(grunt) {

connectTest: {
options: {
port: 8080
port: 8080,
middleware: function(connect, options, middlewares) {
middlewares.push(['/foo', function(req, res, next) {
res.end('bar');
}]);
return middlewares;
}
}
},

Expand Down
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ grunt.initConfig({
### Overview

Provides middleware for the [grunt-contrib-connect](https://github.com/gruntjs/grunt-contrib-connect) task to run a web server.
This task will configure the `connect` task target with the same name and invoke it with the provided arguments.

This task will configure the `connect` task target with the same name and invoke it with the provided arguments.
As this task creates it's own middleware, the `connect` task's `base` option will not be respected. If you want to modify the middleware, provide a function callback as `middleware` option (see [here](https://github.com/gruntjs/grunt-contrib-connect#middleware)). The function will be called after the middleware has been created.

### Options

Expand Down Expand Up @@ -401,7 +401,38 @@ openui5_connect: {
resources: 'path/to/openui5/resources'
}
}
}
```

#### Custom middleware

This example will add custom middleware before (unshift) and after (push) the middlewares created by this task.

```js
connect: {
server: {
options: {
port: 8000,
middleware: function(connect, options, middlewares) {
middlewares.unshift(function(req, res, next) {
// before openui5 middleware
});
middlewares.push(function(req, res, next) {
// after openui5 middleware
});
return middlewares;
}
}
}
},

openui5_connect: {
server: {
options: {
appresources: 'webapp',
resources: 'path/to/openui5/resources'
}
}
}
```

Expand Down
8 changes: 8 additions & 0 deletions tasks/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ module.exports = function(grunt, config) {
// Make sure the same target is configured for the 'connect' task
this.requiresConfig(['connect', this.target]);

// save original middleware object before overwriting it
var vOriginalMiddleware = grunt.config(['connect', target, 'options', 'middleware']) || grunt.config(['connect', 'options', 'middleware']);

// Adopt connect middleware
grunt.config(['connect', target, 'options', 'middleware'], function(connect, connectOptions, middlewares) {
middlewares = []; // clear existing middlewares
Expand Down Expand Up @@ -143,6 +146,11 @@ module.exports = function(grunt, config) {
mountMiddleware(openui5.connect.proxy(typeof options.proxyOptions === 'object' ? options.proxyOptions : undefined), options.proxypath);
}

// run original middleware function
if (typeof vOriginalMiddleware === 'function') {
middlewares = vOriginalMiddleware.call(this, connect, connectOptions, middlewares);
}

return middlewares;
});

Expand Down
6 changes: 6 additions & 0 deletions test/connect_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ describe('openui5 connect should return status 200 ok for', function() {

});

it('a file from custom connect middleware', function(fnDone) {

fnStatusOkTestCase('/foo', fnDone);

});

});

describe('openui5 connect should return status 404 not found for', function() {
Expand Down

0 comments on commit 1cab971

Please sign in to comment.