-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (37 loc) · 1.21 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
const express = require('express');
const path = require('path');
const dotenv = require("dotenv");
const fetch = require("node-fetch");
dotenv.config();
const app = express();
const directory = __dirname;
// Serve static files from the React app
app.use(express.static(path.join(directory, '/client/build')));
// Put all API endpoints under '/api'
app.get('/api/getMovies', async (req, res) => {
console.log(`Request received for /api/getMovies`);
let movies;
try {
const type = 'movie';
const s = req.query.search;
await fetch(`${'http://www.omdbapi.com/?'
+ 'apikey='}${process.env.OMDB_KEY
}&type=${type
}&s=${s}`)
.then((response) => response.json())
.then((response) => { movies = response })
} catch (error) {
console.error(error);
}
console.log(`Sending response for /api/getMovies`);
res.json(movies);
});
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(directory + '/client/build/index.html'));
});
const port = process.env.PORT || 5000;
const server = app.listen(port);
console.log(`Express app listening on ${port}`)
module.exports = server;