-
Notifications
You must be signed in to change notification settings - Fork 55
/
app.js
54 lines (41 loc) · 1.37 KB
/
app.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
'use strict';
// server
const express = require('express');
const app = express();
const path = require('path');
const tmpDir = __dirname + '/tmp/';
const publicDir = __dirname + '/public/';
// canvas generator
const CountdownGenerator = require('./countdown-generator');
app.use(express.static(publicDir));
app.use(express.static(tmpDir));
// root
app.get('/', function (req, res) {
res.sendFile(publicDir + 'index.html');
});
// generate and download the gif
app.get('/generate', function (req, res) {
let {time, width, height, color, bg, name, frames} = req.query;
if(!time){
throw Error('Time parameter is required.');
}
CountdownGenerator.init(time, width, height, color, bg, name, frames, () => {
let filePath = tmpDir + name + '.gif';
res.download(filePath);
});
});
// serve the gif to a browser
app.get('/serve', function (req, res) {
let {time, width, height, color, bg, name, frames} = req.query;
if(!time){
throw Error('Time parameter is required.');
}
CountdownGenerator.init(time, width, height, color, bg, name, frames, () => {
let filePath = tmpDir + name + '.gif';
res.sendFile(filePath);
});
});
app.listen(process.env.PORT || 3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
module.exports = app;