diff --git a/config/index.js b/config/index.js index 7e09b5ee7..e57a04042 100644 --- a/config/index.js +++ b/config/index.js @@ -72,6 +72,7 @@ var config = { debug: false }, + disturl: 'http://dist.u.qiniudn.com', logoURL: 'http://ww4.sinaimg.cn/large/69c1d4acgw1ebfly5kjlij208202oglr.jpg', registryHost: 'r.cnpmjs.org', customFooter: '', // you can add copyright and site total script html here diff --git a/controllers/web/dist.js b/controllers/web/dist.js new file mode 100644 index 000000000..790f9d217 --- /dev/null +++ b/controllers/web/dist.js @@ -0,0 +1,23 @@ +/**! + * cnpmjs.org - controllers/web/dist.js + * + * Copyright(c) cnpmjs.org and other contributors. + * MIT Licensed + * + * Authors: + * fengmk2 (http://fengmk2.github.com) + */ + +"use strict"; + +/** + * Module dependencies. + */ + +var config = require('../../config'); + +exports.redirect = function *(next) { + var params = this.params; + var url = config.disturl + (params[0] || '/'); + this.redirect(url); +}; diff --git a/routes/web.js b/routes/web.js index 316776d85..05dfa6984 100644 --- a/routes/web.js +++ b/routes/web.js @@ -1,4 +1,4 @@ -/*! +/**! * cnpmjs.org - routes/web.js * * Copyright(c) cnpmjs.org and other contributors. @@ -19,6 +19,7 @@ var pkg = require('../controllers/web/package'); var user = require('../controllers/web/user'); var sync = require('../controllers/sync'); var total = require('../controllers/total'); +var dist = require('../controllers/web/dist'); function routes(app) { app.get('/total', total.show); @@ -34,6 +35,8 @@ function routes(app) { app.get('/sync', pkg.displaySync); app.get('/_list/search/search', pkg.rangeSearch); + + app.get(/^\/dist(\/.+)?/, dist.redirect); } module.exports = routes; diff --git a/test/controllers/web/dist.test.js b/test/controllers/web/dist.test.js new file mode 100644 index 000000000..8a9f4e537 --- /dev/null +++ b/test/controllers/web/dist.test.js @@ -0,0 +1,37 @@ +/**! + * cnpmjs.org - test/controllers/web/dist.test.js + * + * Copyright(c) cnpmjs.org and other contributors. + * MIT Licensed + * + * Authors: + * fengmk2 (http://fengmk2.cnpmjs.org) + */ + +'use strict'; + +/** + * Module dependencies. + */ + +var should = require('should'); +var request = require('supertest'); +var app = require('../../../servers/web'); + +describe('controllers/web/dist.test.js', function () { + before(function (done) { + app.listen(0, done); + }); + after(function (done) { + app.close(done); + }); + + describe('GET /dist', function (done) { + it('should 302 to config.disturl', function (done) { + request(app) + .get('/dist') + .expect('Location', 'http://dist.u.qiniudn.com/') + .expect(302, done); + }); + }); +});