moving to koa 2.0; + api
This commit is contained in:
1 parent
44724132c0
commit
0ffa403b60
17 files changed
+398
-339
No files matched your search
+20
-17
@@ -1,7 +1,8 @@
|
||||
import path from 'path';
|
||||
import koa from 'koa';
|
||||
import Koa from 'koa';
|
||||
import serve from 'koa-static';
|
||||
import logger from 'koa-logger';
|
||||
import convert from 'koa-convert';
|
||||
import conditional from 'koa-conditional-get';
|
||||
import etag from 'koa-etag';
|
||||
import prerender from 'koa-prerender';
|
||||
@@ -11,40 +12,42 @@ import bodyParser from 'koa-bodyparser';
|
||||
import config from './config';
|
||||
import {redirectionTable} from './config';
|
||||
import {routes, queryObject} from './routes';
|
||||
// import api from './routes/api';
|
||||
import api from './routes/api';
|
||||
import {redirect} from './routes/redirect';
|
||||
|
||||
const dirPublic = path.join(__dirname, '../public');
|
||||
const dirTemplates = path.join(__dirname, 'templates');
|
||||
|
||||
export function server(){
|
||||
const app = koa();
|
||||
const $ = convert;
|
||||
|
||||
const app = new Koa();
|
||||
app.proxy = true;
|
||||
|
||||
app.use(bodyParser());
|
||||
app.use(logger());
|
||||
// api(app);
|
||||
app.use(conditional());
|
||||
app.use(etag());
|
||||
app.use($(logger()));
|
||||
app.use(api());
|
||||
app.use($(conditional()));
|
||||
app.use($(etag()));
|
||||
app.use(prerenderRmFragment());
|
||||
app.use(prerender(config.prerender));
|
||||
app.use(serve(dirPublic));
|
||||
app.use(serve(dirTemplates));
|
||||
app.use(redirect(redirectionTable));
|
||||
app.use(helmet());
|
||||
app.use($(prerender(config.prerender)));
|
||||
app.use($(serve(dirPublic)));
|
||||
app.use($(serve(dirTemplates)));
|
||||
app.use($(redirect(redirectionTable)));
|
||||
app.use($(helmet()));
|
||||
app.use(queryObject());
|
||||
routes(app);
|
||||
app.use(routes());
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
function prerenderRmFragment(){
|
||||
return function *(next) {
|
||||
yield* next;
|
||||
return async (ctx, next) => {
|
||||
await next();
|
||||
|
||||
try {
|
||||
let xp = this.response.header['x-prerender'] == 'true';
|
||||
if (xp) this.body = this.body.replace('<meta name="fragment" content="!">', '');
|
||||
let xp = ctx.response.header['x-prerender'] == 'true';
|
||||
if (xp) ctx.body = ctx.body.replace('<meta name="fragment" content="!">', '');
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user