mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-18 14:26:56 +00:00
move subdomain_deploy middleware to src/middleware path
This commit is contained in:
parent
01347dcc0d
commit
99561bb412
@ -86,23 +86,4 @@ think.middleware('rewrite_pathname', http => {
|
||||
pathname = pathname.substr(0, pathname.length - suffix.length);
|
||||
}
|
||||
http.pathname = pathname;
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* sub domain deploy
|
||||
* @param {Object} http){}
|
||||
* @return {}
|
||||
*/
|
||||
think.middleware('subdomain_deploy', http => {
|
||||
let subdomain = http.config('subdomain');
|
||||
if (think.isEmpty(subdomain)) {
|
||||
return;
|
||||
}
|
||||
let hostname = http.hostname.split('.')[0];
|
||||
let value = subdomain[hostname];
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
http.pathname = value + '/' + http.pathname;
|
||||
});
|
||||
24
src/middleware/subdomain_deploy.js
Normal file
24
src/middleware/subdomain_deploy.js
Normal file
@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
/**
|
||||
* subdomain
|
||||
* @type {}
|
||||
*/
|
||||
export default class extends think.middleware.base {
|
||||
/**
|
||||
* run
|
||||
* @return {} []
|
||||
*/
|
||||
run(){
|
||||
let subdomain = this.config('subdomain');
|
||||
if (think.isEmpty(subdomain)) {
|
||||
return;
|
||||
}
|
||||
let http = this.http;
|
||||
let hostname = http.hostname.split('.')[0];
|
||||
let value = subdomain[hostname];
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
http.pathname = value + '/' + http.pathname;
|
||||
}
|
||||
}
|
||||
@ -287,48 +287,7 @@ describe('bootstrap/middleware.js', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
it('subdomain_deploy, subdomain emtpy', function(done){
|
||||
getHttp({
|
||||
subdomain: {},
|
||||
}, {
|
||||
pathname: '/test/welefen.text'
|
||||
}).then(function(http){
|
||||
think.middleware('subdomain_deploy', http).then(function(data){
|
||||
assert.equal(http.pathname, '/test/welefen.text');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
it('subdomain_deploy, subdomain', function(done){
|
||||
getHttp({
|
||||
subdomain: {
|
||||
test: 'test'
|
||||
},
|
||||
}, {
|
||||
hostname: 'www.thinkjs.org',
|
||||
pathname: '/test/welefen.text'
|
||||
}).then(function(http){
|
||||
think.middleware('subdomain_deploy', http).then(function(data){
|
||||
assert.equal(http.pathname, '/test/welefen.text');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
it('subdomain_deploy, subdomain 1', function(done){
|
||||
getHttp({
|
||||
subdomain: {
|
||||
test: 'test'
|
||||
},
|
||||
}, {
|
||||
hostname: 'test.thinkjs.org',
|
||||
pathname: 'welefen.text'
|
||||
}).then(function(http){
|
||||
think.middleware('subdomain_deploy', http).then(function(data){
|
||||
assert.equal(http.pathname, 'test/welefen.text');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
75
test/middleware/subdomain.js
Normal file
75
test/middleware/subdomain.js
Normal file
@ -0,0 +1,75 @@
|
||||
var assert = require('assert');
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var muk = require('muk');
|
||||
|
||||
var _http = require('../_http.js');
|
||||
|
||||
function getHttp(config, options){
|
||||
think.APP_PATH = path.dirname(__dirname) + think.sep + 'testApp';
|
||||
var req = think.extend({}, _http.req);
|
||||
var res = think.extend({}, _http.res);
|
||||
return think.http(req, res).then(function(http){
|
||||
if(config){
|
||||
http._config = config;
|
||||
}
|
||||
if(options){
|
||||
for(var key in options){
|
||||
http[key] = options[key];
|
||||
}
|
||||
}
|
||||
return http;
|
||||
})
|
||||
}
|
||||
|
||||
function execMiddleware(middleware, config, options, data){
|
||||
return getHttp(config, options).then(function(http){
|
||||
return think.middleware(middleware, http, data);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
describe('middleware/subdomain', function(){
|
||||
it('subdomain_deploy, subdomain emtpy', function(done){
|
||||
getHttp({
|
||||
subdomain: {},
|
||||
}, {
|
||||
pathname: '/test/welefen.text'
|
||||
}).then(function(http){
|
||||
think.middleware('subdomain_deploy', http).then(function(data){
|
||||
assert.equal(http.pathname, '/test/welefen.text');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
it('subdomain_deploy, subdomain', function(done){
|
||||
getHttp({
|
||||
subdomain: {
|
||||
test: 'test'
|
||||
},
|
||||
}, {
|
||||
hostname: 'www.thinkjs.org',
|
||||
pathname: '/test/welefen.text'
|
||||
}).then(function(http){
|
||||
think.middleware('subdomain_deploy', http).then(function(data){
|
||||
assert.equal(http.pathname, '/test/welefen.text');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
it('subdomain_deploy, subdomain 1', function(done){
|
||||
getHttp({
|
||||
subdomain: {
|
||||
test: 'test'
|
||||
},
|
||||
}, {
|
||||
hostname: 'test.thinkjs.org',
|
||||
pathname: 'welefen.text'
|
||||
}).then(function(http){
|
||||
think.middleware('subdomain_deploy', http).then(function(data){
|
||||
assert.equal(http.pathname, 'test/welefen.text');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user