import express from 'express';
import bodyParser from 'body-parser';
import fs from 'fs';
import dotenv from 'dotenv';
import multer from 'multer';
import InvertedIndex from '../src/inverted-index';


const app = express();

const index = new InvertedIndex();
const upload = multer({ dest: '../fixtures' });

dotenv.config();

const NODE_ENV = process.env.NODE_ENV;

if (NODE_ENV === 'PROD') {
  app.set('PORT', process.env.PORT_PROD);
} else if (NODE_ENV === 'DEV') {
  app.set('PORT', process.env.PORT_DEV);
} else {
  app.set('PORT', process.env.PORT_TEST);
}

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/api/create', upload.array('books'), (req, res) => {
  const content = (req.files);
  content.forEach((file, fileIndex) => {
    const fileName = file.originalname;
    const path = file.path;
    fs.readFile(path, 'utf8', (err, data) => {
      if (err) {
        res.send(err);
      }
      const fileContent = JSON.parse(data);
      fs.unlink(path);
      try {
        const creates = index.createIndex(fileName, fileContent);
        if (fileIndex === content.length - 1) {
          res.json(creates);
        }
      } catch (err) {
        res.send('must create index');
      }
    });
  });
});

app.post('/api/search', (req, res) => {
  res.send(index.isJson(req.body));
});
const port = 1337;
const server = app.listen(process.env.PORT || port, () => console.log(`LISTENING ON PORT ${port}...`));
export default server;

@@ -1302,41 +1302,363 @@
 %7B%0A  
-res.send(index.isJson(req.body));
+const fileName = req.body.fileName;%0A  const terms = req.body.terms;%0A  if ((fileName === undefined && terms === undefined) %7C%7C%0A  (fileName === ('' %7C%7C %5B%5D) && terms === ('' %7C%7C %5B%5D))) %7B%0A    res.send('Please supply a fileName and search-term(s) or only search-term(s)');%0A  %7D else %7B%0A    const output = index.searchIndex(fileName, terms);%0A    res.send(output);%0A  %7D
 %0A%7D);

Fix searchIndex