<commit_before>const express = require.main.require('express')
const path = require('path')
const os = require('os')
const PaperUtils = require('./view/paperutils')
const CIESubjects = require('./view/CIESubjects.js')
const sspdf = require('./lib/sspdf')
const fs = require('fs')
const cheerio = require('cheerio')
const crypto = require('crypto')
const assert = require('assert')
const postJsonReceiver = require('./lib/post-json-receiver.js')
require('./dist-server/serverrender')
const serverRender = global.serverRender
global.serverRender = null
const mongoose = require.main.require('mongoose')
const state2meta = require('./view/state2meta.js')
const Recognizer = require('./lib/recognizer.js')
const ParseQuery = require('./lib/parseQuery.js')

let indexPath = path.join(__dirname, 'dist/index.html')
let indexHtml = fs.readFileSync(indexPath, {encoding: 'utf8'})
if (process.env.NODE_ENV !== 'production') {
  fs.watch(indexPath, list => {
    fs.readFile(indexPath, { encoding: 'utf8' }, (err, data) => {
      if (err) {
        console.error(err)
        process.exit(1)
      } else {
        indexHtml = data
      }
    })
  })
}

module.exports = ({mongodb: db, elasticsearch: es, siteOrigin}) => {
  if (!siteOrigin) siteOrigin = 'https://schsrch.xyz'
  let rMain = express.Router()

  require('./lib/dbModel.js')(db, es).then(({PastPaperDoc, PastPaperIndex, PastPaperFeedback, PastPaperRequestRecord, PastPaperCollection, PastPaperId}) => {
    function statusInfo () {
      return Promise.all([PastPaperDoc.count({}), PastPaperIndex.count({}), PastPaperRequestRecord.count({})])
        .then(([docCount, indexCount, requestCount]) => {
          return Promise.resolve({docCount, indexCount, requestCount})
        }, err => Promise.reject(err))
    }

    rMain.use(function (req, res, next) {
      if (req.hostname.match(/^www\./)) {
        res.redirect(siteOrigin + req.path)
      } else {
        let org
        if ((org = req.get('Origin'))) {
          res.set('Access-Control-Allow-Origin', org)
        }
        next()
      }
    })<commit_msg>Handle CORS correctly, allowing only GET-ish methods<commit_after>const express = require.main.require('express')
const path = require('path')
const os = require('os')
const PaperUtils = require('./view/paperutils')
const CIESubjects = require('./view/CIESubjects.js')
const sspdf = require('./lib/sspdf')
const fs = require('fs')
const cheerio = require('cheerio')
const crypto = require('crypto')
const assert = require('assert')
const postJsonReceiver = require('./lib/post-json-receiver.js')
require('./dist-server/serverrender')
const serverRender = global.serverRender
global.serverRender = null
const mongoose = require.main.require('mongoose')
const state2meta = require('./view/state2meta.js')
const Recognizer = require('./lib/recognizer.js')
const ParseQuery = require('./lib/parseQuery.js')

let indexPath = path.join(__dirname, 'dist/index.html')
let indexHtml = fs.readFileSync(indexPath, {encoding: 'utf8'})
if (process.env.NODE_ENV !== 'production') {
  fs.watch(indexPath, list => {
    fs.readFile(indexPath, { encoding: 'utf8' }, (err, data) => {
      if (err) {
        console.error(err)
        process.exit(1)
      } else {
        indexHtml = data
      }
    })
  })
}

module.exports = ({mongodb: db, elasticsearch: es, siteOrigin}) => {
  if (!siteOrigin) siteOrigin = 'https://schsrch.xyz'
  let rMain = express.Router()

  require('./lib/dbModel.js')(db, es).then(({PastPaperDoc, PastPaperIndex, PastPaperFeedback, PastPaperRequestRecord, PastPaperCollection, PastPaperId}) => {
    function statusInfo () {
      return Promise.all([PastPaperDoc.count({}), PastPaperIndex.count({}), PastPaperRequestRecord.count({})])
        .then(([docCount, indexCount, requestCount]) => {
          return Promise.resolve({docCount, indexCount, requestCount})
        }, err => Promise.reject(err))
    }

    rMain.use(function (req, res, next) {
      if (req.hostname.match(/^www\./)) {
        res.redirect(siteOrigin + req.path)
      } else {
        let org
        if ((org = req.get('Origin'))) {
          let corsMethod = req.get('Access-Control-Request-Method')
          if (method.toUpperCase() === 'GET' || method.toUpperCase() === 'HEAD') {
            res.set('Access-Control-Allow-Origin', org)
            res.set('Access-Control-Allow-Credentials', 'false')
            res.set('Access-Control-Allow-Methods', 'GET, HEAD')
          }
        }
        next()
      }
    })
{'fraction_non_alphanumeric': 0.16651825467497774, 'fraction_numerical': 0.002226179875333927, 'mean_word_length': 3.5429726996966635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1473440', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1373, 'n_words': 277}