-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (59 loc) · 2.08 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const express = require('express');
const linkPreview = require('@nunkisoftware/link-preview');
const mCache = require('memory-cache');
const cors = require('cors');
const app = express();
// Apply cors to provide asynchronous access from browsers.
app.use(cors());
// Validation middleware to simply check the url query param.
const validate = function (req, res, next) {
const url = req.query.url;
if (!url) {
res.status(400).json({ message: 'url query param missing.' });
return;
}
next();
};
// Function which returns an in memory cache middleware.
const cache = function (duration) {
return function (req, res, next) {
const key = req.query.url;
// Try to get cached response using url param as key.
const cachedResponse = mCache.get(key);
if (cachedResponse) {
// Send cached response.
res.json(cachedResponse);
return;
}
// If cached response not present,
// pass the request to the actual handler.
res.originalJSON = res.json;
res.json = function (result) {
// Cache the newly generated response for later use
// and send it to the client.
mCache.put(key, result, duration * 1000);
res.originalJSON(result);
};
next();
};
};
// Actual get handler with cache set to 3 minutes.
app.get('/', validate, cache(180), function (req, res) {
const url = req.query.url;
// Get the actual response from link-preview.
// Wait for 5 secs before calling a timeout.
linkPreview(url, 5000)
.then(function (response) {
if (!response.title) {
// If the url given is incorrect.
res.status(400).json({ message: 'Invalid URL given or timeout occured.' });
return;
}
res.json(response);
})
.catch(function (err) {
res.status(500).send('Internal Server Error.');
});
});
// Listen on the port provided by Up.
app.listen(process.env.PORT || 3000);