Skip to content

Commit

Permalink
Merge pull request #1 from Eliah-Lakhin/master
Browse files Browse the repository at this point in the history
Support custom middlewares
  • Loading branch information
Gusakov Nikita committed Jan 26, 2014
2 parents 98124a8 + 7706108 commit c4b2609
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ gulp.task('serve', serve('public'));
gulp.task('serve-build', serve(['public', 'build']));
gulp.task('serve-prod', serve({
root: ['public', 'build'],
port: 80
port: 80,
middleware: function(req, res) {
// custom optional middleware
}
}));
```

Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ module.exports = function (config) {
config.root = [config.root];
}

if (!config.middlewares) {
config.middlewares = [];
}
if (config.middleware) {
config.middlewares.push(config.middleware);
}
config.middlewares.forEach(function(middleware) {
app.use(middleware);
});

config.root.forEach(function (path) {
app.use(connect.static(path));
});
Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@
"connect"
],
"author": "Gusakov Nikita",
"contributors": [
{
"name": "Ilya Lakhin",
"email": "eliah.lakhin@gmail.com",
"url": "http://lakhin.com/"
}
],
"license": "MIT",
"readmeFilename": "README.md",
"dependencies": {
"gulp-util": "~2.2.12",
"connect": "~2.12.0"
},
"devDependencies": {
"mocha": "~1.17.0",
"should": "~3.1.0",
"connect-livereload": "~0.3.2"
},
"scripts": {
"test": "mocha"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/modified-response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html><body>test-string
<script type="text/javascript">document.write('<script src="' + (location.protocol || 'http:') + '//' + (location.hostname || 'localhost') + ':35729/livereload.js?snipver=1" type="text/javascript"><\/script>')</script>
</body></html>
1 change: 1 addition & 0 deletions test/fixtures/raw-response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>test-string</body></html>
53 changes: 53 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var serve = require('../');
var http = require('http');
var fs = require('fs');
var should = require('should');

require('mocha');

describe('static webserver', function() {
describe('serving', function() {
this.timeout(10000);

it('should serve content', function(done) {
var fixture = fs.readFileSync('./test/fixtures/raw-response.txt').toString();

serve('./test/server/')();

http.get('http://localhost:3000/', function(res) {
res.on('data', function(body) {
body.toString().should.equal(fixture);

done();
});
});
});

it('should be able to modify serving content', function(done) {
var fixture = fs.readFileSync('./test/fixtures/modified-response.txt').toString();

serve({
root: './test/server/',
port: 3001,
middleware: require('connect-livereload')({port: 35729})
})();

http
.request(
{
hostname: 'localhost',
port: 3001,
method: 'GET',
headers: {'accept': 'text/html'}
},
function(res) {
res.on('data', function(body) {
body.toString().should.equal(fixture);
done();
});
}
)
.end();
});
});
});
1 change: 1 addition & 0 deletions test/server/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>test-string</body></html>

0 comments on commit c4b2609

Please sign in to comment.