forked from vuejsdevelopers/vuejs-cinema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (40 loc) · 1.3 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
require('dotenv').config({ silent: true });
const express = require('express');
const app = express();
const path = require('path');
const fs = require('fs');
const api = require('./api');
if (process.env.NODE_ENV === 'development') {
require('./webpack-dev-middleware').init(app);
}
if (process.env.NODE_ENV === 'production') {
app.use('/dist', express.static(path.join(__dirname, 'dist')));
}
app.use('/public', express.static(path.join(__dirname, 'public')));
let template = fs.readFileSync(path.resolve('./index.html'), 'utf-8');
app.get('/', function(req, res) {
res.send(template);
});
app.get('/api', function(req, res) {
api.getData(function(err, data) {
if (err) {
res.status(500).send(err);
} else {
res.json(data);
}
});
});
let offlineData = JSON.parse(fs.readFileSync(path.resolve('./api_offline.json'), 'utf-8'));
app.get('/offline_api', function(req, res) {
let data = offlineData.find(item => item.imdbID === req.query.i);
if (!data) {
data = { "Response":"False", "Error":`IMDb ID ${req.query.i} not found.` }
}
res.json(data);
});
app.listen(process.env.PORT, function () {
console.log(`Example app listening on port ${process.env.PORT}!`);
if (process.env.NODE_ENV === 'development') {
require('opn')(`http://localhost:${process.env.PORT}`);
}
});