forked from AdeleD/react-paginate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (50 loc) · 1.68 KB
/
server.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
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var express = require('express');
var serveStatic = require('serve-static');
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var WebpackConfig = require('./webpack.config');
var app = (module.exports.app = exports.app = express());
var ROOT_DIR = path.join(__dirname, '.');
var STYLES_DIR = path.join(__dirname, 'styles');
var DATA = path.join(__dirname, 'data', 'data.json');
var NODE_PORT = process.env.NODE_PORT || 3000;
var NODE_ENV = process.env.NODE_ENV || 'development';
var PER_PAGE = 10;
app.use(serveStatic(ROOT_DIR));
app.use(serveStatic(STYLES_DIR));
app.use(
webpackDevMiddleware(webpack(WebpackConfig), {
publicPath: '/build/',
stats: {
colors: true,
},
})
);
function getPaginatedItems(items, offset) {
return items.slice(offset, offset + PER_PAGE);
}
app.get('/comments', function(req, res) {
var items = JSON.parse(fs.readFileSync(DATA));
var offset = req.query.offset ? parseInt(req.query.offset, 10) : 0;
var nextOffset = offset + PER_PAGE;
var previousOffset = offset - PER_PAGE < 1 ? 0 : offset - PER_PAGE;
var meta = {
limit: PER_PAGE,
next: util.format('?limit=%s&offset=%s', PER_PAGE, nextOffset),
offset: req.query.offset,
previous: util.format('?limit=%s&offset=%s', PER_PAGE, previousOffset),
total_count: items.length,
};
var json = {
meta: meta,
comments: getPaginatedItems(items, offset),
};
return res.json(json);
});
app.listen(NODE_PORT, function() {
console.log('Demo server running on %s mode on port %d', NODE_ENV, NODE_PORT); // eslint-disable-line
});