-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (52 loc) · 1.38 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
var express = require('express'),
path = require('path'),
jade = require('jade'),
config = require('./config'),
http = require('http');
var app = new express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
// Shared options
var options = {
host: config.wpUrl,
port: 80,
method: 'GET'
};
app.get('/',function(req,res){
options.path = '/wp-json/wp/v2/posts/';
http.request(options, function(resp) {
var docs;
resp.setEncoding('utf8');
resp.on('data', function (chunk) {
if (docs === undefined){
docs = chunk;
} else{
docs +=chunk;
}
});
resp.on('end', function(){
var jdocs = JSON.parse(docs);
res.render('allPosts', {posts:jdocs, title: "All WordPress Posts"});
});
}).end();
});
app.get('/:id',function(req,res){
options.path = '/wp-json/wp/v2/posts/' + req.params.id;
http.request(options, function(resp) {
var docs;
resp.setEncoding('utf8');
resp.on('data', function (chunk) {
if (docs === undefined){
docs = chunk;
} else{
docs +=chunk;
}
});
resp.on('end', function(){
var jdocs = JSON.parse(docs);
res.render('postID', {post:jdocs, title: "Single WordPress Post"});
});
}).end();
});
app.listen(config.listen)