-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (107 loc) · 3.65 KB
/
app.js
File metadata and controls
128 lines (107 loc) · 3.65 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* jshint node: true, devel: true */
'use strict';
require('dotenv').config(); // A simple ".env" file with credential: APP_ID=xxx \n APP_SECRET=yyyyyy
const
bodyParser = require('body-parser'),
express = require('express'),
favicon = require('serve-favicon'),
app = express(),
http = require('http').Server(app),
api = require('./api');
const
PORT = 3000,
oneMonth = 86400000 * 30;
app.set('port', PORT);
app.set('trust proxy', 1);
app.set('view engine', 'ejs');
///////////////////////////// Middleware /////////////////////////////
//app.use(express.static(__dirname + '/public'));
app.use(favicon('./public/img/favicon.ico'));
app.use(bodyParser.json());
///////////////////////////// Server Routes /////////////////////////////
app.get('/epg-channels/', function(req, res) {
res.json(api.CHANNELS);
});
app.get('/epg-programme/', function(req, res) {
var options = {
// mode: 'full', // Full infos ?
// timeStamp: timeStamp,
// channel: findChannel,
// genres: findGenres,
// recherche: findRecherche,
// character: findCharacter,
// ...
limit: 500,
recherche: req.param('title') || '',
longSummary: req.param('summary') || '',
page: parseInt(req.param('page')) || 1
};
api.getEpgPayload(function(programmes, total) {
res.setHeader('X-total', total || 0);
res.json(programmes);
}, options);
});
app.get('/epg/*', function(req, res) { // https://bytel.tv/node/epg/144650041
if (!req.params || !req.params['0']) { // req.params { '0': '145860879' }
return res.status(200).send('Il manque le paramètre "eventId" dans l\'URL');
}
api.getEpgMovie(function(payload) {
if (!payload || !payload.programInfo) {
payload = {};
res.status(404);
}
res.setHeader('Cache-Control', 'public, max-age='+oneMonth);
res.render('fiche_programme', payload);
}, parseInt(req.params['0'])); // req.params['0'] == eventId
});
///////////////////////////// INDEX /////////////////////////////
app.use('/', function(req, res) {
res.send(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Index</title>
</head>
<body>
<div class="container">
<div class="row">
<h1>Index for sample API Cloud APP</h1>
<p>Channels list:
<br />
<a href="./epg-channels">./epg-channels</a></p>
<p>TV Programmes list:
<br />
<a href="./epg-programme">./epg-programme</a></p>
<p>One programme (Need an existing eventID, take one in the fresh list):
<br />
<a href="./epg/185758633">./epg/185758633</a></p>
</div>
</div>
</body>
</html>`);
});
///////////////////////////// 404 ! /////////////////////////////
app.use(function(req, res) { // Handle 404 error
res
.status(404)
.send(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>404</title>
</head>
<body>
<div class="container">
<div class="row">\
<h1>404: Page not Found :(</h1>\
<br />\
<h4>Retour à l'<a href="https://127.0.0.1:${PORT}">Accueil</a></h4>\
</div>
</div>
</body>
</html>`);
});
///////////////////////////// Start Server App /////////////////////////////
http.listen(PORT, function() {
console.log('App.js is running on https://127.0.0.1:'+ PORT);
});