-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_orientdb.js
More file actions
80 lines (71 loc) · 1.98 KB
/
app_orientdb.js
File metadata and controls
80 lines (71 loc) · 1.98 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express');
const bodyParser = require('body-parser');
const OrientDB = require('orientjs');
const app = express();
app.locals.pretty = true;
//orientDB
var server = OrientDB({
host: 'localhost',
port: 2424,
username: 'root',
password: 'dlekdms!@#$'
});
var db = server.use('nodejs');
console.log('Using Database:', db.name);
//pug (jade)
app.set('view engine', 'pug');
app.set('views', './views_orientdb');
//bodyParser (post method)
app.use(bodyParser.urlencoded({extended:false}));
//route의 순서 중요
app.get('/topic/add', function (req, res) {
var sql = 'SELECT FROM topic';
db.query(sql).then(function (topics) {
// if(topics.length === 0) {
// showError('There is no topic record.', res);
// }
res.render('add', {topics:topics});
});
});
app.post('/topic/add', function (req, res) {
var title = req.body.title;
var description = req.body.description;
var author = req.body.author;
var sql = 'INSERT INTO topic (title, description, autor) VALUES(:title, :desc, :author)';
db.query(sql, {
params:{
title: title,
desc: description,
author: author
}
}).then(function (results) {
res.redirect('/topic/' + encodeURIComponent(results[0]['@rid']));
});
});
app.get(['/topic', '/topic/:id'], function (req, res) {
var sql = 'SELECT FROM topic';
db.query(sql).then(function (topics) {
var id = req.params.id;
if(id) {
var sql2 = 'SELECT FROM topic WHERE @rid=:rid';
var decodeId = decodeURIComponent(id);
db.query(sql2, {
params:{
rid:decodeId
}
}).then(function (topic) {
console.log('title : ' + topic.title);
res.render('view', {topics:topics, topic:topic});
});
} else {
res.render('view', {topics: topics});
}
});
});
app.listen(3000, function() {
console.log('Connected 3000 port!');
});
function showError(err, res) {
console.log(err);
res.status(500).send('Internal Server Error');
}