<commit_before>import { promisifyAll } from 'bluebird'
import jwt from 'jsonwebtoken'
import express from 'express'

import { load as configLoader } from '../config/config'
import { dbAdapter } from './models'

import AttachmentsRoute from './routes/api/v1/AttachmentsRoute'
import BookmarkletRoute from './routes/api/v1/BookmarkletRoute'
import CommentsRoute from './routes/api/v1/CommentsRoute'
import GroupsRoute from './routes/api/v1/GroupsRoute'
import PasswordsRoute from './routes/api/v1/PasswordsRoute'
import PostsRoute from './routes/api/v1/PostsRoute'
import SessionRoute from './routes/api/v1/SessionRoute'
import TimelinesRoute from './routes/api/v1/TimelinesRoute'
import UsersRoute from './routes/api/v1/UsersRoute'

import GroupsRouteV2 from './routes/api/v2/GroupsRoute'
import RequestsRouteV2 from './routes/api/v2/RequestsRoute'
import SearchRoute from './routes/api/v2/SearchRoute'
import TimelinesRouteV2 from './routes/api/v2/TimelinesRoute'
import UsersRouteV2 from './routes/api/v2/UsersRoute'
import StatsRouteV2 from './routes/api/v2/Stats'

const config = configLoader();
promisifyAll(jwt);

export default function (app) {
  const findUser = async (req, res, next) => {
    const authToken = req.headers['x-authentication-token']
      || req.body.authToken
      || req.query.authToken;

    if (authToken) {
      try {
        const decoded = await jwt.verifyAsync(authToken, config.secret);
        const user = await dbAdapter.getUserById(decoded.userId);

        if (user) {
          req.user = user;
        }
      } catch (e) {
        app.logger.info(`invalid token. the user will be treated as anonymous: ${e.message}`);
      }
    }

    next();
  };

  app.use(express.static(`${__dirname}/../${config.attachments.storage.rootDir}`));

  // unauthenticated routes
  app.options('/*', (req, res) => {
    res.status(200).send({})
  });
  PasswordsRoute(app);
  SessionRoute(app);

  // [at least optionally] authenticated routes
  app.all('/*', findUser);
  AttachmentsRoute(app);
  BookmarkletRoute(app);
  CommentsRoute(app);
  GroupsRoute(app);
  PostsRoute(app);
  TimelinesRoute(app);
  UsersRoute(app);
  StatsRouteV2(app);

  GroupsRouteV2(app);
  RequestsRouteV2(app);
  SearchRoute(app);
  TimelinesRouteV2(app);
  UsersRouteV2(app);
}<commit_msg>Return a valid JSON response on call to nonexistent API methods<commit_after>import { promisifyAll } from 'bluebird'
import jwt from 'jsonwebtoken'
import express from 'express'

import { load as configLoader } from '../config/config'
import { dbAdapter } from './models'

import AttachmentsRoute from './routes/api/v1/AttachmentsRoute'
import BookmarkletRoute from './routes/api/v1/BookmarkletRoute'
import CommentsRoute from './routes/api/v1/CommentsRoute'
import GroupsRoute from './routes/api/v1/GroupsRoute'
import PasswordsRoute from './routes/api/v1/PasswordsRoute'
import PostsRoute from './routes/api/v1/PostsRoute'
import SessionRoute from './routes/api/v1/SessionRoute'
import TimelinesRoute from './routes/api/v1/TimelinesRoute'
import UsersRoute from './routes/api/v1/UsersRoute'

import GroupsRouteV2 from './routes/api/v2/GroupsRoute'
import RequestsRouteV2 from './routes/api/v2/RequestsRoute'
import SearchRoute from './routes/api/v2/SearchRoute'
import TimelinesRouteV2 from './routes/api/v2/TimelinesRoute'
import UsersRouteV2 from './routes/api/v2/UsersRoute'
import StatsRouteV2 from './routes/api/v2/Stats'

const config = configLoader();
promisifyAll(jwt);

export default function (app) {
  const findUser = async (req, res, next) => {
    const authToken = req.headers['x-authentication-token']
      || req.body.authToken
      || req.query.authToken;

    if (authToken) {
      try {
        const decoded = await jwt.verifyAsync(authToken, config.secret);
        const user = await dbAdapter.getUserById(decoded.userId);

        if (user) {
          req.user = user;
        }
      } catch (e) {
        app.logger.info(`invalid token. the user will be treated as anonymous: ${e.message}`);
      }
    }

    next();
  };

  app.use(express.static(`${__dirname}/../${config.attachments.storage.rootDir}`));

  // unauthenticated routes
  app.options('/*', (req, res) => {
    res.status(200).send({})
  });
  PasswordsRoute(app);
  SessionRoute(app);

  // [at least optionally] authenticated routes
  app.all('/*', findUser);
  AttachmentsRoute(app);
  BookmarkletRoute(app);
  CommentsRoute(app);
  GroupsRoute(app);
  PostsRoute(app);
  TimelinesRoute(app);
  UsersRoute(app);
  StatsRouteV2(app);

  GroupsRouteV2(app);
  RequestsRouteV2(app);
  SearchRoute(app);
  TimelinesRouteV2(app);
  UsersRouteV2(app);

  app.all('/v[0-9]+/*', (req, res) => res.status(404).send({ err: `API method not found: '${req.path}'` }));
}
{'fraction_non_alphanumeric': 0.14228667507356033, 'fraction_numerical': 0.01282051282051282, 'mean_word_length': 3.9521331945889697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4281284', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1553, 'n_words': 338}