|
|
@@ -1,8 +1,11 @@
|
|
|
import express from 'express';
|
|
|
import dotenv from 'dotenv'
|
|
|
+import fs from 'fs';
|
|
|
import { saveImage, scrapeCritters, CONFIG, storeData } from './scraper';
|
|
|
|
|
|
const app: express.Application = express();
|
|
|
+const publicDataPath = `${__dirname}/../public/data`;
|
|
|
+const publicImagesPath = `${__dirname}/../public/images`;
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
|
@@ -11,6 +14,12 @@ if (process.env.AC_FISH === undefined || process.env.AC_INSECT === undefined) {
|
|
|
process.exit();
|
|
|
}
|
|
|
|
|
|
+const errorHandler: express.ErrorRequestHandler = (err, req, res, next) => {
|
|
|
+ console.error(err);
|
|
|
+ res.status(500);
|
|
|
+ res.send({ error: err });
|
|
|
+}
|
|
|
+
|
|
|
app.use(express.static('public', {
|
|
|
setHeaders: function setHeaders(res, path, stat) {
|
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
|
@@ -29,18 +38,22 @@ app.use((req, res, next)=> {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+app.use(errorHandler);
|
|
|
app.get('/', (req, res) => { res.send('Hello World!') });
|
|
|
|
|
|
|
|
|
const getFishes = () => {
|
|
|
- scrapeCritters({url: process.env.AC_FISH!, type: CONFIG.Fish}).then(saveImage('images/fishes/')).then(storeData(`${__dirname}/public/data/fishes.json`));
|
|
|
+ scrapeCritters({url: process.env.AC_FISH!, type: CONFIG.Fish}).then(saveImage('images/fishes/')).then(storeData(`${publicDataPath}/fishes.json`));
|
|
|
}
|
|
|
|
|
|
const getInsects = () => {
|
|
|
- scrapeCritters({url:process.env.AC_INSECT!, type: CONFIG.Insect}).then(saveImage('images/insects/')).then(storeData(`${__dirname}/public/data/insects.json`))
|
|
|
+ scrapeCritters({url:process.env.AC_INSECT!, type: CONFIG.Insect}).then(saveImage('images/insects/')).then(storeData(`${publicDataPath}/insects.json`))
|
|
|
}
|
|
|
|
|
|
app.listen(process.env.PORT, () => {
|
|
|
+ [publicDataPath, publicImagesPath].map(path => {
|
|
|
+ fs.mkdirSync(path, {recursive: true})
|
|
|
+ })
|
|
|
getFishes();
|
|
|
getInsects();
|
|
|
console.log(`Scrapper app listening at http://localhost:${process.env.PORT}`);
|