-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const queries = require("./queries");
const cache = require("./utils/cache");
const app = express();
app.use(morgan("short")); // Connection logging
app.set("trust proxy", 1);
const port = process.env.PORT || 3002;
// https://stackoverflow.com/questions/24897801/enable-access-control-allow-origin-for-multiple-domains-in-node-js
app.use((req, res, next) => {
const origin = req.headers.origin;
if (process.env.ALLOWED_ORIGINS.includes(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
return next();
});
app.get("/", queries.root);
/**
* Returns the top/popular search results
*/
app.get("/search/", cache(86400), queries.getHomePageResults);
/**
* Returns search results based on searchTerm
*/
app.get("/search/:searchTerm", cache(86400), queries.search);
/**
* Returns all streaming services for a title and the title's info
*/
app.get("/title/:type/:titleId", cache(86400), queries.getTitleStreamingServices);
/**
* Returns all locales and returns only country and full_locale
* Likely not necessary as it's already called in /providers below
* https://apis.justwatch.com/content/locales/state
*/
app.get("/locales", queries.getLocales);
/**
* Returns all locales and their providers
* Only run when allLocalesAndProviders.json needs to be updated
* THIS IS NOT NECESSARY ANYMORE AFTER GRAPHQL MIGRATION
* https://apis.justwatch.com/content/providers/locale/${full_locale}
*/
app.get("/providers", queries.getAllProviders);
app.listen(port, () => {
console.log(`global-justwatch-api started on port ${port}`);
});