var express = require('express')
var Crudivore = require('./crudivore')
var _ = require('lodash')
var app = express()

function start(config, onSuccess) {
  var render = Crudivore(config)

  app.set('port', (process.env.PORT || 5000))

  var server = app.listen(app.get('port'), function() {
    console.log('Running on port ' + app.get('port'))
    if (onSuccess) onSuccess()
  })

  app.get('/render/*', function(req, res) {
    var pageUrl = decodeURIComponent(req.params[0])
    console.log('Requesting page ',  pageUrl)

    var renderingResult = render.renderPage(pageUrl)
    renderingResult.onError(function(e) {
      console.log(e)
      res.status(400).end()
    })

    renderingResult.onValue(function(result) {
      _(result.headers).each(function(value, name) {
        res.setHeader(name, value)
      })
      res.status(result.status).send(result.content)
    })
  })

  app.get('/info/', function(req, res) {
    res.send(render.threadInfo())
  })

  return server
}

module.exports = start

var express = require('express')
var Crudivore = require('./crudivore')
var _ = require('lodash')
var app = express()

function start(config, onSuccess) {
  var render = Crudivore(config)

  app.set('port', (process.env.PORT || 5000))

  var server = app.listen(app.get('port'), function() {
    console.log('Running on port ' + app.get('port'))
    if (onSuccess) onSuccess()
  })

  app.get('/render/*', function(req, res) {
    var pageUrl = decodeURIComponent(req.params[0])
    console.log('Requesting page ',  pageUrl)

    var renderingResult = render.renderPage(pageUrl)
    renderingResult.onError(function(e) {
      console.log(e)
      res.status(500).end()
    })

    renderingResult.onValue(function(result) {
      _(result.headers).each(function(value, name) {
        res.setHeader(name, value)
      })
      res.status(result.status).send(result.content)
    })
  })

  app.get('/info/', function(req, res) {
    res.send(render.threadInfo())
  })

  return server
}

module.exports = start

Return http status 500 instead of 400 on errors
Return http status 500 instead of 400 on errors

400 implies that there is a problem with the request. 500 is an internal error.
