var express = require('express');
//var apicache = require('apicache').options({ debug: true }).middleware;
var app = express();
var spawn = require('child_process').spawn;
var path = require('path');

var fs = require('fs');
var settings = JSON.parse(fs.readFileSync(path.resolve(__dirname, './settings.json'), 'utf8'));

var punScript = path.resolve(__dirname, './pun.py');

function run(command, args, callback) {
  console.log(command, args);
  var child = spawn(command, args);
  var response = '';
  var error = '';
  child.stdout.on('data', function(buffer) {
    response += buffer.toString();
  });
  child.stderr.on('data', function(buffer) {
    error += buffer.toString();
  });
  child.on('close', function(code) {
    if (code === 0) {
      callback(response);
    } else {
      callback(null, error);
    }
  });
}

console.log('registering /');
app.get('/' /*, apicache('5 minutes')*/, function(req, res) {
  console.log('GET ' + req.originalUrl);
  if (settings.origin) {
    res.setHeader('Access-Control-Allow-Origin', settings.origin);
  }

  res.set({
    'Content-Type': 'application/json'
  });

  run(punScript, [], function(punResponse, err) {
    if (err) {
      console.error(err);
      res.status(500);
      res.send({"error": err});
      return;
    }

    if (punResponse) {
      punResponse = punResponse.trim();
    }

    res.status(200);
    res.send({"pun": punResponse});
  });
});

var port = settings.port;
app.listen(port);
console.log('listening on ' + port);


@@ -1147,24 +1147,16 @@
 tion(pun
-Response
 , err) %7B
@@ -1235,16 +1235,38 @@
 es.send(
+req.accepts('json') ? 
 %7B%22error%22
@@ -1271,16 +1271,22 @@
 r%22: err%7D
+ : err
 );%0A     
@@ -1312,24 +1312,16 @@
  if (pun
-Response
 ) %7B%0A    
@@ -1329,30 +1329,14 @@
  pun
-Response = punResponse
+ = pun
 .tri
@@ -1381,16 +1381,38 @@
 es.send(
+req.accepts('json') ? 
 %7B%22pun%22: 
@@ -1418,17 +1418,15 @@
  pun
-Response%7D
+%7D : pun
 );%0A 

Add request accept check