-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (89 loc) · 3.8 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
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
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const apicache = require("apicache");
const app = express();
const port = process.env.PORT || 4001;
const cache = apicache.middleware
// CORS
var allowedOrigins = ['http://localhost:4001',
'http://localhost:8080',
'https://kural.surge.sh',
'https://kural.surge.sh/',
'https://kural.tamilwords.net/',
'https://kural.tamilwords.net',
'https://thirukkural.onrender.com/',
];
app.use(cors({
origin: function(origin, callback) {
// allow requests with no origin
// (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg = 'The CORS policy for this site does not ' +
'allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
app.listen(port, function() {
console.log('listening on port ' + port);
});
app.get('/', (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
res.setHeader('Content-Type', 'application/json');
app.disable('x-powered-by');
fs.readFile('kural.json', function(err, buf) {
var qarr = JSON.parse(buf);
var random = qarr.kural[Math.floor(Math.random() * qarr.kural.length)];
res.json(random);
});
});
app.get('/:id', cache('1 hour'), (req, res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
res.setHeader('Content-Type', 'application/json');
app.disable('x-powered-by');
const getID = req.params.id
fs.readFile('kural.json', function(err, buf) {
var qarr = JSON.parse(buf);
var kural_data = qarr.kural.find(el => el.Number == getID);
if (kural_data == undefined) {
const random_data = Math.floor(Math.random() * 1000) + 1
var kural_data = qarr.kural.find(el => el.Number == random_data);
res.json(kural_data);
} else {
res.json(kural_data);
}
});
});
app.use('/', cache('1 hour'), function(req, res) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('X-Frame-Options', 'DENY');
res.header('X-XSS-Protection', '1; mode=block');
res.header('X-Content-Type-Options', 'nosniff');
res.header('Strict-Transport-Security', 'max-age=63072000');
res.setHeader('Content-Type', 'application/json');
app.disable('x-powered-by');
fs.readFile('kural.json', function(err, buf) {
var qarr = JSON.parse(buf);
const random_data = Math.floor(Math.random() * 1000) + 1
var kural_data = qarr.kural.find(el => el.Number == random_data);
res.status(200).json(kural_data);
});
});
module.exports = app;