-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (51 loc) · 1.66 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const NewsItemsRoutes = express.Router();
// importing the json data set for news items
let newsitems = require('./NewsItems.json');
var resObj = {};
app.use(cors());
app.use(bodyParser.json());
/* Rote for returning requested news item and
for blocking previous and next item search
in the client side for first and lat item accordingly */
NewsItemsRoutes.route('/searchNews').post(function(req, res) {
if( req.body.page_filter == 1 )
{
resObj = {
news_items: [newsitems.NewsItems[req.body.page_filter-1]],
current_page: req.body.page_filter,
total_pages: newsitems.NewsItems.length,
has_next : true ,
has_previous : false
}
}
else if( req.body.page_filter == newsitems.NewsItems.length )
{
resObj = {
news_items: [newsitems.NewsItems[req.body.page_filter-1]],
current_page: req.body.page_filter,
total_pages: newsitems.NewsItems.length,
has_next : false ,
has_previous : true
}
}
else
{
resObj = {
news_items: [newsitems.NewsItems[req.body.page_filter-1]],
current_page: req.body.page_filter,
total_pages: newsitems.NewsItems.length,
has_next : true,
has_previous : true
}
}
res.json(resObj);
});
// using routing middleware
app.use('/NewsItems', NewsItemsRoutes);
app.listen(4000, function() {
console.log("Server is running on Port: " + 4000);
});