-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
34 lines (29 loc) · 920 Bytes
/
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
import express from 'express';
import http from 'http';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import { routes } from './routes';
//Express
const app = express();
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.get('*', (req, res) => {
match({ routes, location: req.url }, (err, redirectLocation, props) => {
if (err) {
res.status(500).send(err.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (props) {
const markup = renderToString(<RouterContext {...props} />);
res.render('index', { markup });
} else {
res.sendStatus(404);
}
});
});
const server = http.createServer(app);
server.listen(3003);
server.on('listening', () => {
console.log('Listening on port 3003');
});