{"size": 3363, "ext": "js", "max_stars_repo_path": "src/server.js", "max_stars_repo_name": "josoroma/status", "max_stars_repo_stars_event_min_datetime": "2016-08-22T15:44:54.000Z", "max_stars_repo_stars_event_max_datetime": "2016-08-22T15:44:54.000Z", "max_issues_repo_path": "src/server.js", "max_issues_repo_name": "josoroma/status", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/server.js", "max_forks_repo_name": "josoroma/status", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.7279411765, "max_line_length": 103, "alphanum_fraction": 0.6416889682}
import express from 'express';
import bodyParser from 'body-parser';
import path from 'path';

import { renderToString } from 'react-dom/server'

import StatusStore from '../src/stores/StatusStore';
import ViewStore from '../src/stores/ViewStore';
import StatusApp from '../src/components/statusApp.js';
import React from 'react';

global.navigator = { userAgent: 'all' };

const app = express();
app.use('/node_modules', express.static(path.join(__dirname, '../node_modules')));

const webpack = require('webpack');
// Wrapper middleware for webpack. It serves the files emitted from webpack
// over a connect server.
// https://github.com/webpack/webpack-dev-middleware
const webpackDevMiddleware = require('webpack-dev-middleware');
// To connect a browser client to a webpack server & receive updates.
// https://github.com/glenjamin/webpack-hot-middleware
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('../webpack.config');
const compiler = webpack(config);

app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
app.use(webpackHotMiddleware(compiler));

const renderFullPage = html => {
  const initialState = { statuses };

  return `
    <!doctype html>
    <html lang="utf-8">
      <head>
        <script>
          window.initialState = ${JSON.stringify(initialState)}
        </script>
      </head>
      <body>
        <section id="app-status" class="app-status">
          <div>
            ${html}
          </div>
        </section>
        <script src="/static/bundle.js"></script>
      </body>
    </html>
  `
};

// Statuses are going to be stored here.
let statuses = [
  {
    id: 1,
    title: `A Spoonful of Sugar.`,
    friend: false
  },
  {
    id: 2,
    title: `A box of chocolates.`,
    friend: false
  },
  {
    id: 3,
    title: `Funny pictures of people shopping.`,
    friend: false
  },
  {
    id: 4,
    title: `More dark chocolate.`,
    friend: true
  },
  {
    id: 5,
    title: `A good cup of coffee.`,
    friend: true
  },
  {
    id: 6,
    title: `Once upon a time...`,
    friend: true
  }
];

app.use(bodyParser.json());

app.get('/', function(req, res) {
  const statusStore = StatusStore.fromJS(statuses);
  const viewStore = new ViewStore();

  const initView = renderToString((
    <StatusApp statusStore={statusStore} viewStore={viewStore} />
  ));

  const page = renderFullPage(initView);

  res.status(200).send(page);
});

app.post('/api/statuses', function(req, res) {
  statuses = req.body.statuses;

  if (Array.isArray(statuses)) {
    console.log(`Updated statuses (${statuses.length})`);
    res.status(201).send(JSON.stringify({ success: true }));
  } else {
    res.status(200).send(JSON.stringify({ success: false, error: "expected `statuses` to be array" }));
  }
});

// example of handling 404 pages
app.get('*', function(req, res) {
  console.error('- 404 -\n');

  res.status(404).send('404 - Page Not Found');
});

// global error catcher, need four arguments
app.use((err, req, res, next) => {
  console.error('Error on request %s %s\n', req.method, req.url);
  console.error(err.stack + '\n');

  res.status(500).send("Server error");
});

process.on('uncaughtException', evt => {
  console.log('Exception: ', evt);
});

app.listen(3000, function() {
  console.log('http://localhost:3000' + '\n');
});
