-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
47 lines (38 loc) · 1 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
'use strict';
var express = require('express'),
bodyParser = require('body-parser'),
Realm = require('realm');
var app = express();
let PostSchema = {
name: 'Post',
properties: {
timestamp: 'date',
title: 'string',
content: 'string'
}
};
var blogRealm = new Realm({
path: 'blog.realm',
schema: [PostSchema]
});
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
let posts = blogRealm.objects('Post').sorted('timestamp', true);
res.render('index.ejs', {posts: posts});
});
app.get('/write', function(req, res) {
res.sendFile(__dirname + "/write.html");
});
app.post('/write', function(req, res) {
let title = req.body['title'],
content = req.body['content'],
timestamp = new Date();
blogRealm.write(() => {
blogRealm.create('Post', {title: title, content: content, timestamp: timestamp});
});
res.sendFile(__dirname + "/write-complete.html");
});
app.listen(3000, function() {
console.log("Go!");
});