forked from andreypopp/react-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (42 loc) · 1.21 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
'use strict';
var path = require('path');
var url = require('url');
var express = require('express');
var browserify = require('connect-browserify');
var ReactAsync = require('react-async');
var nodejsx = require('node-jsx').install({extension: '.jsx'});
var App = require('./client.jsx');
var development = process.env.NODE_ENV !== 'production';
function renderApp(req, res, next) {
var path = url.parse(req.url).pathname;
var app = App({path: path});
ReactAsync.renderToStringAsync(app, function(err, markup) {
if (err) {
return next(err);
}
res.send('<!doctype html>\n' + markup);
});
}
var api = express()
.get('/users/:username', function(req, res) {
var username = req.params.username;
res.send({
username: username,
name: username.charAt(0).toUpperCase() + username.slice(1)
});
});
var app = express();
if (development) {
app.get('/assets/bundle.js',
browserify('./client.jsx', {
debug: true,
watch: true
}));
}
app
.use('/assets', express.static(path.join(__dirname, 'assets')))
.use('/api', api)
.use(renderApp)
.listen(3000, function() {
console.log('Point your browser at http://localhost:3000');
});