-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
45 lines (39 loc) · 1.35 KB
/
index.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
const env = process.env.NODE_ENV || 'development';
const config = require('./config')[env];
const handler = require('./handler');
const bodyParser = require('body-parser');
const express = require('express');
const mustacheExpress = require('mustache-express');
const server = express();
const CronJob = require('cron').CronJob;
const localStorage = require('localStorage');
const port = config.server.port;
const baseUrl = config.server.base_url;
server.engine('html', mustacheExpress());
server.set('view engine', 'html');
server.set('views', __dirname + '/views');
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));
server.use(express.static(__dirname + '/public'));
server.post('/shorten', function(req,res) {
localStorage.setItem('fullUrl',req.body.fullUrl);
handler.main(req.body.fullUrl)
setTimeout(function () {
res.redirect('/');
}, 1000);
});
server.get('/', function(req,res) {
let urls = {
"fullUrl": localStorage.getItem('fullUrl'),
"shortUrl": localStorage.getItem('shortUrl'),
"urls": JSON.parse(localStorage.getItem('urls')),
"baseUrl": baseUrl,
}
res.render('index',urls);
});
server.listen(port, () => {
console.log('server is listening on port: '+port);
});
new CronJob('0 0 * * *', function() {
handler.dbCleanup()
}, null, true, 'Asia/Jakarta');