mysociety/popit-api

View on GitHub
src/versions/1.0.0.js

Summary

Maintainability
A
3 hrs
Test Coverage
"use strict";

var express = require('express');
var reIndex = require('../reindex');

module.exports = popitApiApp;

// Expose the reIndex function so popit UI can use it.
popitApiApp.reIndex = reIndex;

function popitApiApp(options) {
  var app = express();

  require('../app-setup')(app, options);


  require('../routes/info')(app, options);
  require('../routes/export')(app);
  require('../routes/collection.param')(app);
  require('../routes/1.0/search')(app);
  require('../routes/1.0/collection.get')(app);
  require('../routes/1.0/collection.post')(app);
  require('../routes/merge')(app);
  require('../routes/image')(app);
  require('../routes/1.0/document.get')(app);
  require('../routes/document.delete')(app);
  require('../routes/1.0/document.put')(app);

  // Error handling
  app.use(function(err, req, res, next) {
    // This should always be an error, but jshint complains if we don't use all
    // arguments and express will only interpret this as an error handler if
    // the arity is 4.
    if (!err) {
      return next();
    }
    console.error(err.stack);
    res.status(500).send({errors: ["Something went wrong", err.message]});
  });

  /*
    Anything else we should 400 for as it is probably an unsupported method.
  */
  app.all('*', function (req, res) {
    res.status(405).jsonp({errors: ["unsupported method"] });
  });

  return app;
}