generated from wyncode/midterm_project_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (43 loc) · 1.25 KB
/
server.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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const API_KEY = process.env.API_KEY;
const express = require('express');
const path = require('path');
const app = express();
const axios = require('axios');
// JUST FOR DEMO PURPOSES, PUT YOUR ACTUAL API CODE HERE
const getOmdbAPI = async (search) => {
try {
const resp = await axios.get(
`http://www.omdbapi.com/?apikey=${process.env.API_KEY}&t='${search}'`
);
return resp;
} catch (e) {
console.log(e.message);
}
};
app.get('/ombd', async (request, response) => {
const { search } = request.query;
console.log(search);
try {
const movieData = await getOmdbAPI(search);
response.json(movieData.data);
// response.json("Hello WOrld");
} catch (error) {
console.log(error.message);
}
});
// END DEMO
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', (request, response) => {
response.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`API listening on port ${port}...`);
});