Showing
6 changed files
with
23 additions
and
126 deletions
1 | -var path = require('path') | 1 | +var path = require('path'); |
2 | +var pkg = require("../../package.json"); | ||
2 | 3 | ||
3 | module.exports = { | 4 | module.exports = { |
4 | build: { | 5 | build: { |
5 | env: { | 6 | env: { |
6 | NODE_ENV: '"production"' | 7 | NODE_ENV: '"production"' |
7 | }, | 8 | }, |
8 | - index: path.resolve(__dirname, '../../server/public/index.html'), | 9 | + index: path.resolve(__dirname, `./bundle/${pkg.version}/index.html`), |
9 | assetsRoot: path.resolve(__dirname, './bundle'), | 10 | assetsRoot: path.resolve(__dirname, './bundle'), |
10 | assetsSubDirectory: 'static', | 11 | assetsSubDirectory: 'static', |
11 | - assetsPublicPath: 'http://shop-manage.yohobuy.com/', | 12 | + assetsPublicPath: 'https://cdn.yoho.cn/yoho-shop-manage/', |
12 | productionSourceMap: true, | 13 | productionSourceMap: true, |
13 | productionGzip: false, | 14 | productionGzip: false, |
14 | productionGzipExtensions: ['js', 'css'], | 15 | productionGzipExtensions: ['js', 'css'], |
@@ -9,7 +9,7 @@ const bodyParser = require('body-parser'); | @@ -9,7 +9,7 @@ const bodyParser = require('body-parser'); | ||
9 | const cookieParser = require('cookie-parser'); | 9 | const cookieParser = require('cookie-parser'); |
10 | const compression = require('compression'); | 10 | const compression = require('compression'); |
11 | const Express = require('express'); | 11 | const Express = require('express'); |
12 | -const history = require('./framework/history-api-fallback'); | 12 | +const pkg = require('../package.json'); |
13 | 13 | ||
14 | // const session = require('express-session'); | 14 | // const session = require('express-session'); |
15 | const cookieSession = require('cookie-session'); | 15 | const cookieSession = require('cookie-session'); |
@@ -22,21 +22,13 @@ const helpers = require('yoho-node-lib/lib/helpers'); | @@ -22,21 +22,13 @@ const helpers = require('yoho-node-lib/lib/helpers'); | ||
22 | 22 | ||
23 | let app = new Express(); | 23 | let app = new Express(); |
24 | 24 | ||
25 | -global.Promise = require('bluebird'); | ||
26 | - | ||
27 | global.yoho = { | 25 | global.yoho = { |
28 | logger, | 26 | logger, |
29 | helpers, | 27 | helpers, |
30 | config, | 28 | config, |
31 | apiDomain: config.apiDomain, | 29 | apiDomain: config.apiDomain, |
32 | - co: global.Promise.coroutine | 30 | + version: pkg.version |
33 | }; | 31 | }; |
34 | -app.use(history({ | ||
35 | - disableIndex: true, | ||
36 | - rewrites: [ | ||
37 | - { from: /\.html/, to: '/index.html'} | ||
38 | - ] | ||
39 | -})); | ||
40 | app.use(cookieSession({ | 32 | app.use(cookieSession({ |
41 | name: 'yoho-shop', | 33 | name: 'yoho-shop', |
42 | secret: 'yoho!shop@manage', | 34 | secret: 'yoho!shop@manage', |
@@ -56,6 +48,8 @@ const middleware = require('./middleware'); | @@ -56,6 +48,8 @@ const middleware = require('./middleware'); | ||
56 | const controllers = require('./controllers'); | 48 | const controllers = require('./controllers'); |
57 | 49 | ||
58 | try { | 50 | try { |
51 | + app.use(middleware.main); | ||
52 | + | ||
59 | // 前置中间件 | 53 | // 前置中间件 |
60 | app.use(middleware.before); | 54 | app.use(middleware.before); |
61 | 55 |
@@ -20,5 +20,4 @@ router.post('/logout', middleware(UserController, 'logout')); | @@ -20,5 +20,4 @@ router.post('/logout', middleware(UserController, 'logout')); | ||
20 | router.post('/upload/image', multipartMiddleware, middleware(FileController, 'uploadImage')); | 20 | router.post('/upload/image', multipartMiddleware, middleware(FileController, 'uploadImage')); |
21 | router.post('/upload/xlsx', multipartMiddleware, middleware(FileController, 'uploadXlsx')); | 21 | router.post('/upload/xlsx', multipartMiddleware, middleware(FileController, 'uploadXlsx')); |
22 | 22 | ||
23 | - | ||
24 | module.exports = router; | 23 | module.exports = router; |
1 | -/* eslint-disable */ | ||
2 | -var url = require('url'); | ||
3 | - | ||
4 | -exports = module.exports = function historyApiFallback(options) { | ||
5 | - options = options || {}; | ||
6 | - var logger = getLogger(options); | ||
7 | - | ||
8 | - return function (req, res, next) { | ||
9 | - var headers = req.headers; | ||
10 | - if (req.method !== 'GET') { | ||
11 | - logger( | ||
12 | - 'Not rewriting', | ||
13 | - req.method, | ||
14 | - req.url, | ||
15 | - 'because the method is not GET.' | ||
16 | - ); | ||
17 | - return next(); | ||
18 | - } else if (!headers || typeof headers.accept !== 'string') { | ||
19 | - logger( | ||
20 | - 'Not rewriting', | ||
21 | - req.method, | ||
22 | - req.url, | ||
23 | - 'because the client did not send an HTTP accept header.' | ||
24 | - ); | ||
25 | - return next(); | ||
26 | - } else if (headers.accept.indexOf('application/json') === 0) { | ||
27 | - logger( | ||
28 | - 'Not rewriting', | ||
29 | - req.method, | ||
30 | - req.url, | ||
31 | - 'because the client prefers JSON.' | ||
32 | - ); | ||
33 | - return next(); | ||
34 | - } else if (!acceptsHtml(headers.accept, options)) { | ||
35 | - logger( | ||
36 | - 'Not rewriting', | ||
37 | - req.method, | ||
38 | - req.url, | ||
39 | - 'because the client does not accept HTML.' | ||
40 | - ); | ||
41 | - return next(); | ||
42 | - } | ||
43 | - | ||
44 | - var parsedUrl = url.parse(req.url); | ||
45 | - var rewriteTarget; | ||
46 | - options.rewrites = options.rewrites || []; | ||
47 | - | ||
48 | - for (var i = 0; i < options.rewrites.length; i++) { | ||
49 | - var rewrite = options.rewrites[i]; | ||
50 | - var match = parsedUrl.pathname.match(rewrite.from); | ||
51 | - if (match !== null) { | ||
52 | - rewriteTarget = evaluateRewriteRule(parsedUrl, match, rewrite.to); | ||
53 | - logger('Rewriting', req.method, req.url, 'to', rewriteTarget); | ||
54 | - req.url = rewriteTarget; | ||
55 | - return next(); | ||
56 | - } | ||
57 | - } | ||
58 | - | ||
59 | - if (parsedUrl.pathname.indexOf('.') !== -1 && | ||
60 | - options.disableDotRule !== true) { | ||
61 | - logger( | ||
62 | - 'Not rewriting', | ||
63 | - req.method, | ||
64 | - req.url, | ||
65 | - 'because the path includes a dot (.) character.' | ||
66 | - ); | ||
67 | - return next(); | ||
68 | - } | ||
69 | - | ||
70 | - if (options.disableIndex) { | ||
71 | - return next(); | ||
72 | - } | ||
73 | - | ||
74 | - rewriteTarget = options.index || '/index.html'; | ||
75 | - logger('Rewriting', req.method, req.url, 'to', rewriteTarget); | ||
76 | - req.url = rewriteTarget; | ||
77 | - next(); | ||
78 | - }; | ||
79 | -}; | ||
80 | - | ||
81 | -function evaluateRewriteRule(parsedUrl, match, rule) { | ||
82 | - if (typeof rule === 'string') { | ||
83 | - return rule; | ||
84 | - } else if (typeof rule !== 'function') { | ||
85 | - throw new Error('Rewrite rule can only be of type string of function.'); | ||
86 | - } | ||
87 | - | ||
88 | - return rule({ | ||
89 | - parsedUrl: parsedUrl, | ||
90 | - match: match | ||
91 | - }); | ||
92 | -} | ||
93 | - | ||
94 | -function acceptsHtml(header, options) { | ||
95 | - options.htmlAcceptHeaders = options.htmlAcceptHeaders || ['text/html', '*/*']; | ||
96 | - for (var i = 0; i < options.htmlAcceptHeaders.length; i++) { | ||
97 | - if (header.indexOf(options.htmlAcceptHeaders[i]) !== -1) { | ||
98 | - return true; | ||
99 | - } | ||
100 | - } | ||
101 | - return false; | ||
102 | -} | ||
103 | - | ||
104 | -function getLogger(options) { | ||
105 | - if (options && options.logger) { | ||
106 | - return options.logger; | ||
107 | - } else if (options && options.verbose) { | ||
108 | - return console.log.bind(console); | ||
109 | - } | ||
110 | - return function () { }; | ||
111 | -} |
@@ -8,5 +8,6 @@ const before = require('./before'); | @@ -8,5 +8,6 @@ const before = require('./before'); | ||
8 | const auth = require('./auth'); | 8 | const auth = require('./auth'); |
9 | const proxy = require('./proxy'); | 9 | const proxy = require('./proxy'); |
10 | const error = require('./error'); | 10 | const error = require('./error'); |
11 | +const main = require('./main'); | ||
11 | 12 | ||
12 | -module.exports = {before, auth, proxy, error}; | 13 | +module.exports = {before, auth, proxy, error, main}; |
server/middleware/main.js
0 → 100644
1 | +const Express = require('express'); | ||
2 | +let router = Express.Router(); // eslint-disable-line | ||
3 | +const request = require('request'); | ||
4 | +const version = global.yoho.version; | ||
5 | + | ||
6 | +let mainProxy = (req, res) => { | ||
7 | + request.get(`https://cdn.yoho.cn/yoho-shop-manage/${version}/index.html`).pipe(res); | ||
8 | +}; | ||
9 | + | ||
10 | +router.get('/', mainProxy); | ||
11 | +router.get(/\.html$/, mainProxy); | ||
12 | + | ||
13 | +module.exports = router; |
-
Please register or login to post a comment