Authored by 陈峰

发布配置

var path = require('path')
var path = require('path');
var pkg = require("../../package.json");
module.exports = {
build: {
env: {
NODE_ENV: '"production"'
},
index: path.resolve(__dirname, '../../server/public/index.html'),
index: path.resolve(__dirname, `./bundle/${pkg.version}/index.html`),
assetsRoot: path.resolve(__dirname, './bundle'),
assetsSubDirectory: 'static',
assetsPublicPath: 'http://shop-manage.yohobuy.com/',
assetsPublicPath: 'https://cdn.yoho.cn/yoho-shop-manage/',
productionSourceMap: true,
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
... ...
... ... @@ -9,7 +9,7 @@ const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const Express = require('express');
const history = require('./framework/history-api-fallback');
const pkg = require('../package.json');
// const session = require('express-session');
const cookieSession = require('cookie-session');
... ... @@ -22,21 +22,13 @@ const helpers = require('yoho-node-lib/lib/helpers');
let app = new Express();
global.Promise = require('bluebird');
global.yoho = {
logger,
helpers,
config,
apiDomain: config.apiDomain,
co: global.Promise.coroutine
version: pkg.version
};
app.use(history({
disableIndex: true,
rewrites: [
{ from: /\.html/, to: '/index.html'}
]
}));
app.use(cookieSession({
name: 'yoho-shop',
secret: 'yoho!shop@manage',
... ... @@ -56,6 +48,8 @@ const middleware = require('./middleware');
const controllers = require('./controllers');
try {
app.use(middleware.main);
// 前置中间件
app.use(middleware.before);
... ...
... ... @@ -20,5 +20,4 @@ router.post('/logout', middleware(UserController, 'logout'));
router.post('/upload/image', multipartMiddleware, middleware(FileController, 'uploadImage'));
router.post('/upload/xlsx', multipartMiddleware, middleware(FileController, 'uploadXlsx'));
module.exports = router;
... ...
/* eslint-disable */
var url = require('url');
exports = module.exports = function historyApiFallback(options) {
options = options || {};
var logger = getLogger(options);
return function (req, res, next) {
var headers = req.headers;
if (req.method !== 'GET') {
logger(
'Not rewriting',
req.method,
req.url,
'because the method is not GET.'
);
return next();
} else if (!headers || typeof headers.accept !== 'string') {
logger(
'Not rewriting',
req.method,
req.url,
'because the client did not send an HTTP accept header.'
);
return next();
} else if (headers.accept.indexOf('application/json') === 0) {
logger(
'Not rewriting',
req.method,
req.url,
'because the client prefers JSON.'
);
return next();
} else if (!acceptsHtml(headers.accept, options)) {
logger(
'Not rewriting',
req.method,
req.url,
'because the client does not accept HTML.'
);
return next();
}
var parsedUrl = url.parse(req.url);
var rewriteTarget;
options.rewrites = options.rewrites || [];
for (var i = 0; i < options.rewrites.length; i++) {
var rewrite = options.rewrites[i];
var match = parsedUrl.pathname.match(rewrite.from);
if (match !== null) {
rewriteTarget = evaluateRewriteRule(parsedUrl, match, rewrite.to);
logger('Rewriting', req.method, req.url, 'to', rewriteTarget);
req.url = rewriteTarget;
return next();
}
}
if (parsedUrl.pathname.indexOf('.') !== -1 &&
options.disableDotRule !== true) {
logger(
'Not rewriting',
req.method,
req.url,
'because the path includes a dot (.) character.'
);
return next();
}
if (options.disableIndex) {
return next();
}
rewriteTarget = options.index || '/index.html';
logger('Rewriting', req.method, req.url, 'to', rewriteTarget);
req.url = rewriteTarget;
next();
};
};
function evaluateRewriteRule(parsedUrl, match, rule) {
if (typeof rule === 'string') {
return rule;
} else if (typeof rule !== 'function') {
throw new Error('Rewrite rule can only be of type string of function.');
}
return rule({
parsedUrl: parsedUrl,
match: match
});
}
function acceptsHtml(header, options) {
options.htmlAcceptHeaders = options.htmlAcceptHeaders || ['text/html', '*/*'];
for (var i = 0; i < options.htmlAcceptHeaders.length; i++) {
if (header.indexOf(options.htmlAcceptHeaders[i]) !== -1) {
return true;
}
}
return false;
}
function getLogger(options) {
if (options && options.logger) {
return options.logger;
} else if (options && options.verbose) {
return console.log.bind(console);
}
return function () { };
}
... ... @@ -8,5 +8,6 @@ const before = require('./before');
const auth = require('./auth');
const proxy = require('./proxy');
const error = require('./error');
const main = require('./main');
module.exports = {before, auth, proxy, error};
module.exports = {before, auth, proxy, error, main};
... ...
const Express = require('express');
let router = Express.Router(); // eslint-disable-line
const request = require('request');
const version = global.yoho.version;
let mainProxy = (req, res) => {
request.get(`https://cdn.yoho.cn/yoho-shop-manage/${version}/index.html`).pipe(res);
};
router.get('/', mainProxy);
router.get(/\.html$/, mainProxy);
module.exports = router;
... ...