-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (56 loc) · 1.69 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const screenshotTweet = require('screenshot-tweet');
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
app.set('views', path.join(__dirname, '.', 'views'));
app.engine('handlebars', exphbs({
defaultLayout: 'main',
layoutsDir: path.join(__dirname, '.', 'views', 'layouts')
}));
app.set('view engine', 'handlebars');
process.env.PWD = process.cwd()
function generateID() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function addhttp(url) {
if (!/^(f|ht)tps?:\/\//i.test(url)) {
url = "https://" + url;
}
return url;
}
function validURL(url){
let regex = /twitter\.com\/\w+_?\/status\/\d+/g;
return regex.test(url);
}
app.get('/', (req, res) => {
res.render('home');
});
app.post('/twit', (req, res) =>{
if(!validURL(req.body.url)) res.send('Invalid url <a href="/">return home</a>');
let id = generateID();
let httpUrl = addhttp(req.body.url);
screenshotTweet.default(
httpUrl,
path.join(process.env.PWD, 'public', 'tweets',id+'.jpg')
)
.then(() => {
res.redirect('/success?img='+id);
})
.catch(error => console.log('Error'+error));
});
app.get('/success', (req, res) => {
let filepath = path.join('tweets',req.query.img+'.jpg');
res.render('success',{filepath});
});
const port = process.env.PORT || 3000;
app.listen(port, ()=> {
console.log('App listening on port '+port);
});