Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,31 @@
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
all: [
'**/*.js',
'<%= mochaTest.test.src %>'
],
options: {
jshintrc: '.jshintrc',
ignores: ['node_modules/**']
}
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/**/*-test.js']
}
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.registerTask('test', ['jshint', 'mochaTest']);
grunt.registerTask('default', ['test']);
};

View File

@ -0,0 +1,22 @@
Copyright (c) 2015 Brent Ertz
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.

View File

@ -0,0 +1,77 @@
# connect-pushstate
Connect middleware that rewrites select requests to the site root, allowing your client-side pushstate router to handle them.
Requests including a file extension are left untouched so site assets like your images, stylesheets, and JavaScripts will load unaffected, while requests without a file extension, presumably pages or actions within your site, are rewritten to point at the root, with the original URL intact.
You can also specify regular expressions to specifically allow or disallow additional paths from being affected.
This functionality is commonly needed by single page web-apps.
## Getting Started
Install package
```shell
npm install connect-pushstate --save
```
Load the middleware by adding the following line of JavaScript.
```js
var pushState = require('connect-pushstate');
```
Add the `pushState` middleware call to your server definition, amongst your other middleware.
##### Options
The pushState method accepts an options object as an optional parameter with the following properties.
* __root__ - The location where requests will be rerouted to. Defaults to '/'. e.g. `pushState({ root: '/foo' })`
* __allow__ - A pattern that will allow requests matching it to pass through without being redirected. e.g. `pushState({ allow: '^/api' })` You might need this option if your client app and API are on the same server.
* __disallow__ - A pattern that will disallow requests matching it to pass through without being redirected. e.g. `pushState({ disallow: '^/version/1.2.3' })`
```js
'use strict';
var connect = require('connect');
var morgan = require('morgan');
var serveStatic = require('serve-static');
var pushState = require('connect-pushstate');
var port = process.env.PORT || 3000;
var app = connect()
.use(pushState())
.use(serveStatic('www/'))
.use(morgan('dev'))
.listen(port, function() {
console.log('Application server stated on port', port);
});
```
_Note that [serve-static](https://www.npmjs.com/package/serve-static) is needed in order to actually serve your files._
## Examples
For a quick demo, see the examples directory, or run the test suite.
```shell
cd examples
node server.js
```
## Tests
Execute the test suite
```
npm test
```
## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
## Release History
- 1.1.0 Add "disallow" option. Add grunt package to devDependencies.
- 1.0.0 Introduce options object. Add "allow" option. Simplify directory structure. Update dependencies. Other minor housekeeping.
- 0.1.0 Initial release

View File

@ -0,0 +1,15 @@
'use strict';
var connect = require('connect');
var morgan = require('morgan');
var serveStatic = require('serve-static');
var pushState = require('../index');
var port = process.env.PORT || 3000;
var app = connect()
.use(pushState())
.use(serveStatic('../test/fixtures/www/'))
.use(morgan('dev'))
.listen(port, function() {
console.log('Application server stated on port', port);
});

View File

@ -0,0 +1,26 @@
'use strict';
var path = require('path');
var url = require('url');
module.exports = function(options) {
options = options || {};
var root = options.root || '/';
var allow = options.allow ? new RegExp(options.allow) : false;
var disallow = options.disallow ? new RegExp(options.disallow) : false;
return function pushState(req, res, next) {
var pathname = url.parse(req.url).pathname;
var allowed = allow ? allow.test(pathname) : false;
var disallowed = disallow ? disallow.test(pathname) : false;
var hasFileExtension = !!(path.extname(pathname));
if (allowed || (!disallowed && hasFileExtension)) {
next();
} else {
req.url = root;
next();
}
};
};

View File

@ -0,0 +1,66 @@
{
"_from": "connect-pushstate@^1.1.0",
"_id": "connect-pushstate@1.1.0",
"_inBundle": false,
"_integrity": "sha1-vKsiQnHEOWBKD7D2FMCl9WPojiQ=",
"_location": "/connect-pushstate",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "connect-pushstate@^1.1.0",
"name": "connect-pushstate",
"escapedName": "connect-pushstate",
"rawSpec": "^1.1.0",
"saveSpec": null,
"fetchSpec": "^1.1.0"
},
"_requiredBy": [
"/budo"
],
"_resolved": "https://registry.npmjs.org/connect-pushstate/-/connect-pushstate-1.1.0.tgz",
"_shasum": "bcab224271c439604a0fb0f614c0a5f563e88e24",
"_spec": "connect-pushstate@^1.1.0",
"_where": "/home/manfred/enviPath/ketcher2/ketcher/node_modules/budo",
"author": {
"name": "Brent Ertz",
"email": "brent.ertz@gmail.com",
"url": "http://brentertz.com"
},
"bugs": {
"url": "https://github.com/brentertz/connect-pushstate/issues"
},
"bundleDependencies": false,
"dependencies": {},
"deprecated": false,
"description": "Connect middleware that rewrites select requests to the site root, allowing your client-side pushstate router to handle them",
"devDependencies": {
"chai": "^1.10.0",
"connect": "^3.3.4",
"grunt": "^1.0.1",
"grunt-contrib-jshint": "^0.10.0",
"grunt-mocha-test": "^0.12.6",
"mocha": "^2.1.0",
"morgan": "^1.5.1",
"request": "^2.51.0",
"serve-static": "^1.8.0"
},
"homepage": "https://github.com/brentertz/connect-pushstate",
"keywords": [
"connect",
"middleware",
"rewrite",
"pushstate",
"routing"
],
"license": "MIT",
"name": "connect-pushstate",
"repository": {
"type": "git",
"url": "git://github.com/brentertz/connect-pushstate.git"
},
"scripts": {
"test": "grunt test"
},
"version": "1.1.0"
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@ -0,0 +1 @@
www/index.html

View File

@ -0,0 +1 @@
www/other/index.html

View File

@ -0,0 +1,102 @@
'use strict';
var expect = require('chai').expect;
var request = require('request');
var connect = require('connect');
var serveStatic = require('serve-static');
var pushState = require('../index');
var www = __dirname + '/fixtures/www';
describe('pushState', function() {
var app = connect()
.use(pushState())
.use(serveStatic(www));
it('calls the next middleware', function(done) {
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(body).to.contain('www/index.html');
server.close(done);
});
});
});
it('rewrites the request url to point at the root when the request does not include a file extension', function(done) {
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000/pathname', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(body).to.contain('www/index.html');
server.close(done);
});
});
});
it('rewrites the request url to point at the root regardless of whether the querystring contains a file extension', function(done) {
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000/pathname/?q=foo.bar', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(body).to.contain('www/index.html');
server.close(done);
});
});
});
it('does not rewrite the request url when the request includes a file extension', function(done) {
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000/images/image.png', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(res.headers['content-type']).to.contain('image/png');
server.close(done);
});
});
});
it('rewrites the request url to point at a custom root if defined', function(done) {
var app = connect()
.use(pushState({ root: '/other/' }))
.use(serveStatic(www));
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000/other/pathname', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(body).to.contain('www/other/index.html');
server.close(done);
});
});
});
it('does not rewrite the request url when specified as allowed', function(done) {
var app = connect()
.use(pushState({ allow: '^/api' }))
.use(serveStatic(www))
.use('/api/users', function(req, res, next) {
res.end('users');
});
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000/api/users', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(body).to.contain('users');
server.close(done);
});
});
});
it('rewrites the request url when specified as disallowed', function(done) {
var app = connect()
.use(pushState({ disallow: '^/version' }))
.use(serveStatic(www))
.use('/version', function(req, res, next) {
res.end('version');
});
var server = app.listen(3000).on('listening', function() {
request('http://0.0.0.0:3000/version/1.2.3', function(err, res, body) {
expect(res.statusCode).to.equal(200);
expect(body).to.contain('www/index.html');
server.close(done);
});
});
});
});