-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (40 loc) · 1.13 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
const express = require('express');
const bodyParser = require('body-parser');
const moment = require('moment');
const posts = [];
const pageSize = 5;
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.get('/', (req, res) => {
let currentPage = 1;
const totalPosts = posts.length;
const pageCount = Math.ceil(totalPosts / pageSize);
if(req.query.page){
currentPage = parseInt(req.query.page, 10);
}
const start = (currentPage - 1) * pageSize;
const end = currentPage * pageSize;
res.render('index',
{
posts: posts.slice(start,end),
pageSize: pageSize,
pageCount: pageCount,
currentPage: currentPage,
}
);
});
app.post('/new', (req, res) => {
const post = {
id: posts.length + 1,
videoID: req.body.videoID,
timestamp: moment().format(),
timestampStr: moment().fformat,
};
posts.unshift(post);
res.status(200).json(post);
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});