Arpith Siromoney ūüí¨

Server Side React + EJS

Yesterday, I mentioned that my app’s webpages (at were being generated by joining strings. This is a way around that, using EJS to place the server side react data into an html template. The template itself is simple:

   <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
   <meta name='viewport' content='width=device-width, initial-scale=1' />
   <link rel='stylesheet' type='text/css' href='/style.css'
   <div id='react-mount'><%- react -%></div>
   <script src='/main.js' type='text/javascript'></script>

All I’ve done is add <%- react -%> where I want my react string to be inserted. I’ve placed this file in a folder called templates, and made a small change to the code that serves the user pages:

var render = require('koa-ejs');
render(app, {root: 'templates'});
app.use(function *() {
 var splitURL = this.url.split('/');
 var username = splitURL.shift().toLowerCase();
 var id = splitURL.shift();
 var user = yield fetchUser(username).then((user) => {
   if ((id) && (user.posts.indexOf(id) > 0)) {
     user.posts.splice(user.posts.indexOf(id), 1);
   var promiseArr = => fetchPost(username, id));
   return Promise.all(promiseArr).then((data) => {
     user.posts = data;
     return user;
 var reactString = ReactDOMServer.renderToString(React.createElement(views.User, user));
 yield this.render('layout', {react: reactString});

Add koa-ejs to your list of dependencies (in your package.json) and Heroku will take care of the rest! This fixes #11.