-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (45 loc) · 1.27 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import koa from 'koa';
import KoaJade from 'koa-jade';
import serve from 'koa-static';
import React from 'react';
import { renderToString } from 'react-dom/server'
import { match, RoutingContext } from 'react-router';
import routes from './routes';
const app = koa();
const jade = new KoaJade({
viewPath: './views',
debug: false,
pretty: false,
compileDebug: false
});
app.use(jade.middleware);
app.use(serve('public'));
// x-response-time
app.use(function*(next) {
var start = new Date;
yield next;
var ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
});
app.use(function*() {
match(
{ routes: routes(), location: this.request.url },
(error, redirectLocation, renderProps) => {
if (error) {
this.response.status = 500;
this.body = error.message;
} else if (redirectLocation) {
this.response.redirect(redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
this.response.status = 200;
const content = renderToString(<RoutingContext {...renderProps} />);
this.render('index', { content: content });
} else {
this.response.status = 404;
this.body = 'Not Found';
}
}
);
});
app.listen(3000);
console.log('App listening on Port 3000');