Skip to content
Browse files

Init.

  • Loading branch information...
0 parents commit ec3f1fdbb9309d21248355d6bc277d1ca6b595dd @bripkens committed May 18, 2013
Showing with 152 additions and 0 deletions.
  1. +18 −0 .gitignore
  2. +12 −0 Gruntfile.js
  3. +21 −0 LICENSE
  4. +1 −0 README.md
  5. +16 −0 lib/index.js
  6. +16 −0 package.json
  7. +68 −0 test/rewrites.js
18 .gitignore
@@ -0,0 +1,18 @@
+node_modules
+
+### Node.gitignore
+
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+npm-debug.log
12 Gruntfile.js
@@ -0,0 +1,12 @@
+module.exports = function(grunt) {
+
+ grunt.loadNpmTasks('grunt-contrib-nodeunit');
+
+ grunt.initConfig({
+ nodeunit: {
+ all: ['test/**/*.js']
+ }
+ });
+
+ grunt.registerTask('default', ['nodeunit']);
+};
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2012 Ben Ripkens http://bripkens.de
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
1 README.md
@@ -0,0 +1 @@
+# Hello!
16 lib/index.js
@@ -0,0 +1,16 @@
+var url = require('url');
+
+exports = module.exports = function historyApiFallback (req, res, next) {
+ var headers = req.headers;
+ if (req.method !== 'GET' || headers.accept.indexOf('text/html') === -1) {
+ return next();
+ }
+
+ var parsedUrl = url.parse(req.url);
+ if (parsedUrl.pathname.indexOf('.') !== -1) {
+ return next();
+ }
+
+ req.url = '/index.html';
+ next();
+};
16 package.json
@@ -0,0 +1,16 @@
+{
+ "name": "connect-history-api-fallback",
+ "version": "0.0.1",
+ "description": "Provides a fallback for non-existing directories so that the HTML 5 history API can be used.",
+ "main": "lib/index.js",
+ "scripts": {
+ "test": "grunt"
+ },
+ "repository": "",
+ "author": "Ben Ripkens",
+ "license": "MIT",
+ "devDependencies": {
+ "grunt": "~0.4.1",
+ "grunt-contrib-nodeunit": "~0.1.2"
+ }
+}
68 test/rewrites.js
@@ -0,0 +1,68 @@
+var historyApiFallback = require('../lib');
+
+var tests = module.exports = {};
+
+var req = null;
+var nextCalled = false;
+var requestedUrl = '/foo';
+var next = function () {
+ nextCalled = true;
+};
+
+
+tests.setUp = function (done) {
+ req = {
+ method: 'GET',
+ url: requestedUrl,
+ headers: {
+ accept: 'text/html, */*'
+ }
+ };
+ nextCalled = false;
+
+ done();
+};
+
+
+['POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'].forEach(function (method) {
+ tests['should ignore ' + method + ' requests'] = function (test) {
+ req.method = method;
+
+ historyApiFallback(req, null, next);
+
+ test.equal(req.url, requestedUrl);
+ test.ok(nextCalled);
+ test.done();
+ };
+});
+
+
+tests['should ignore requests that do not accept html directory'] = function (test) {
+ req.headers.accept = 'application/json';
+
+ historyApiFallback(req, null, next);
+
+ test.equal(req.url, requestedUrl);
+ test.ok(nextCalled);
+ test.done();
+};
+
+
+tests['should ignore file requests'] = function (test) {
+ var expected = req.url = 'js/app.js';
+
+ historyApiFallback(req, null, next);
+
+ test.equal(req.url, expected);
+ test.ok(nextCalled);
+ test.done();
+};
+
+tests['should rewrite valid requests'] = function (test) {
+ historyApiFallback(req, null, next);
+
+
+ test.equal(req.url, '/index.html');
+ test.ok(nextCalled);
+ test.done();
+};

0 comments on commit ec3f1fd

Please sign in to comment.
Something went wrong with that request. Please try again.