Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
redirect /name to /package/name when /name is 404. fixed #245
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Mar 4, 2014
1 parent 4460810 commit d8a76d0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cnpmjs.org
=======

[![Build Status](https://secure.travis-ci.org/cnpm/cnpmjs.org.png)](http://travis-ci.org/cnpm/cnpmjs.org) [![Coverage Status](https://coveralls.io/repos/cnpm/cnpmjs.org/badge.png)](https://coveralls.io/r/cnpm/cnpmjs.org) [![Dependency Status](https://gemnasium.com/cnpm/cnpmjs.org.png)](https://gemnasium.com/cnpm/cnpmjs.org)
[![Build Status](https://secure.travis-ci.org/cnpm/cnpmjs.org.png)](http://travis-ci.org/cnpm/cnpmjs.org) [![Dependency Status](https://gemnasium.com/cnpm/cnpmjs.org.png)](https://gemnasium.com/cnpm/cnpmjs.org)

[![NPM](https://nodei.co/npm/cnpmjs.org.png?downloads=true&stars=true)](https://nodei.co/npm/cnpmjs.org/)

Expand All @@ -11,7 +11,8 @@ cnpmjs.org

Private npm registry and web for Enterprise, base on [koa](http://koajs.com/), MySQL and [Simple Store Service](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide).

@[JacksonTian](https://github.com/JacksonTian/) had a talk about [private npm](https://speakerdeck.com/jacksontian/qi-ye-ji-node-dot-jskai-fa).
* @[dead-horse](https://github.com/dead-horse): [What is cnpm?](http://deadhorse.me/slides/cnpmjs.html)
* @[JacksonTian](https://github.com/JacksonTian/) had a talk about [private npm](https://speakerdeck.com/jacksontian/qi-ye-ji-node-dot-jskai-fa).

![cnpm](https://docs.google.com/drawings/d/12QeQfGalqjsB77mRnf5Iq5oSXHCIUTvZTwECMonqCmw/pub?w=480&h=360)

Expand Down
5 changes: 3 additions & 2 deletions docs/web/readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# cnpmjs.org: Private npm registry and web for Enterprise

[![Build Status](https://secure.travis-ci.org/cnpm/cnpmjs.org.png)](http://travis-ci.org/cnpm/cnpmjs.org) [![Coverage Status](https://coveralls.io/repos/cnpm/cnpmjs.org/badge.png)](https://coveralls.io/r/cnpm/cnpmjs.org) [![Dependency Status](https://gemnasium.com/cnpm/cnpmjs.org.png)](https://gemnasium.com/cnpm/cnpmjs.org)
[![Build Status](https://secure.travis-ci.org/cnpm/cnpmjs.org.png)](http://travis-ci.org/cnpm/cnpmjs.org) [![Dependency Status](https://gemnasium.com/cnpm/cnpmjs.org.png)](https://gemnasium.com/cnpm/cnpmjs.org)

[![NPM](https://nodei.co/npm/cnpmjs.org.png?downloads=true&stars=true)](https://nodei.co/npm/cnpmjs.org/)

## What is this?

> Private npm registry and web for Enterprise, base on [koa](http://koajs.com/), MySQL and [Simple Store Service](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide).
@[JacksonTian](https://github.com/JacksonTian/) had a talk about [private npm](https://speakerdeck.com/jacksontian/qi-ye-ji-node-dot-jskai-fa).
* @[dead-horse](https://github.com/dead-horse): [What is cnpm?](http://deadhorse.me/slides/cnpmjs.html)
* @[JacksonTian](https://github.com/JacksonTian/) had a talk about [private npm](https://speakerdeck.com/jacksontian/qi-ye-ji-node-dot-jskai-fa).

## Install your private npm registry

Expand Down
6 changes: 6 additions & 0 deletions middleware/web_not_found.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module.exports = function *notFound(next) {
if (this.status) {
return;
}

var m = /^\/([\w\-\_\.]+)\/?$/.exec(this.url);
if (m) {
return this.redirect('/package/' + m[1]);
}

this.status = 404;
this.type = 'text/html';
this.charset = 'utf-8';
Expand Down
63 changes: 63 additions & 0 deletions test/middleware/web_not_found.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**!
* cnpmjs.org - test/middleware/web_not_found.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var should = require('should');
var request = require('supertest');
var app = require('../../servers/web');

describe('middleware/web_not_found.test.js', function () {
before(function (done) {
app.listen(0, done);
});

after(function (done) {
app.close(done);
});

describe('web_not_found()', function () {
it('should redirect /byte to /package/byte', function (done) {
request(app)
.get('/byte')
.expect('Location', '/package/byte')
.expect(302, done);
});

it('should redirect /byte/ to /package/byte', function (done) {
request(app)
.get('/byte/')
.expect('Location', '/package/byte')
.expect(302, done);
});

it('should 404 /~byte', function (done) {
request(app)
.get('/~byte')
.expect(404, done);
});

it('should 200 /package/byte', function (done) {
request(app)
.get('/package/byte')
.expect(200, done);
});

it('should 404 /package/byte404', function (done) {
request(app)
.get('/package/byte404')
.expect(404, done);
});
});
});

0 comments on commit d8a76d0

Please sign in to comment.