-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (54 loc) · 1.48 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
var express = require('express');
var app = express();
var Ractive = require('ractive');
var fs = require('fs');
var rr = require('ractive-render');
app.engine('html', rr.renderFile);
app.use(express.static(__dirname + '/'));
process.on('uncaughtException', function (err) {
console.error("[Inside 'uncaughtException' event] " + err.stack || err.message);
});
app.get('/', function (req, res) {
res.end('hi there, I\'m a node server');
});
app.get('/index', function (req, res) {
var data = {
title: "Index",
date: new Date()
};
if (req.headers['x-pjax']) {
res.header('Content-Type', 'application/json');
rr.renderFile('views/indexcontent.html', {data:data}, function(err, html) {
res.end(JSON.stringify({
"data":data,
"tpl":Ractive.parse(html)
}));
});
} else {
app.render('index.html', {data: data}, function(err, html) {
res.end(html);
});
}
});
app.get('/list', function (req, res) {
var data = {
title: "List",
date: new Date(),
list: ['apple', 'banana', 'lemon']
};
if (req.headers['x-pjax']) {
res.header('Content-Type', 'application/json');
rr.renderFile('views/listcontent.html', {data:data}, function(err, html) {
res.end(JSON.stringify({
"data":data,
"tpl":Ractive.parse(html)
}));
});
} else {
app.render('list.html', {data: data}, function(err, html) {
res.end(html);
});
}
});
app.listen(3000);
console.log('Listening on port 3000');