-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
110 lines (89 loc) · 2.95 KB
/
web.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
101
102
103
104
const express = require('express');
const bodyParser = require("body-parser");
const cors = require('cors');
const config = require('config');
const news = require('./news');
const PORT = process.env.PORT || 3030;
const redis = require('redis');
// You need to configure redis for this server to work
const cache_config = {redis:process.env.REDIS_URL || config.get('redis') };
const cache = require("express-redis-cache")({client: redis.createClient(cache_config.redis)});
cache.on("connected", () => {console.log("cache connected")});
cache.on("disconnected", () => {
// .... this is a redis cache
console.log("cache disconneted");
});
// create express app
const app = express();
let setCache = function (req, res, next) {
// here you can define period in second, this one is 12 hours
const period = 60 * 60 * 12;
if (req.method == 'GET'){
res.set('Cache-control', `public, max-age=${period}`)
}else{
res.set('Cache-control', `no-store`)
}
// you only want to cache for GET requests
// remember to call next() to pass on the request
next()
};
// parse requests of content-type - application/x-www-form-urlencoded
// app.use(bodyParser.urlencoded({ extended: true }));
// parse requests of content-type - application/json
// app.use(bodyParser.json());
// adding cors
app.use(cors());
app.use(setCache);
// define a simple route
app.get('/:search', (req, res) => {
res.redirect(`/search/${req.params.search}`);
});
app.get("/search/:searchTerm",(req,res,next) => {
//middle ware to define cache name
//set chache name
res.express_redis_cache_name = "search-" + req.params.searchTerm;
next();},cache.route({expire:36000}),(req,res) => {
const searchTerm = req.params.searchTerm;
news.search(searchTerm).then(response => {
if(response){
res.status(200).json(response);
}else{
res.status(401).json({
message:
"there was an error fetching articles please try again later"
});
}
}).catch(error => {
res.status(401).json({
message: error.message
});
});
});
app.get("/refine/:category",
(req, res, next) => {
//middle ware to define cache name
//set chache name
res.express_redis_cache_name = "refine-" + req.params.category;
next();},cache.route({expire:36000}), (req, res) => {
//destructuring
const category = req.params.category;
news.refine(category).then(response => {
if (response) {
res.status(200).json(response);
} else {
res.status(401).json({
message:
"there was an error fetching articles please try again later"
});
}
}).catch(error => {
res.status(401).json({
message: error.message
});
});
}
);
// listening for requests
app.listen(PORT).on('listening', () => {
console.log(`Articles API Running on ${PORT} `);
});