-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_orientdb.js
81 lines (81 loc) · 2.31 KB
/
app_orientdb.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var express = require('express');
var bodyParser = require('body-parser');
var multer = require('multer');
var _storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
})
var upload = multer({ storage: _storage })
var fs = require('fs');
var OrientDB = require('orientjs');
var server = OrientDB({
host: 'localhost',
port: 2424,
username: 'root',
password: 'victorica7'
});
var db = server.use('o2');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.locals.pretty = true;
app.use('/user', express.static('uploads'));
app.set('views', './views_orientdb');
app.set('view engine', 'jade');
app.get('/upload', function(req, res){
res.render('upload');
})
app.post('/upload', upload.single('userfile'), function(req, res){
console.log(req.file);
res.send('uploaded : '+req.file.filename);
})
app.get('/topic/add', function(req, res){
var sql = 'SELECT FROM topic';
db.query(sql).then(function(topics){
if(topics.length === 0){
console.log('There is no topic record');
res.status(500).send('Internal Server Error');
}
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, author) VALUES(:title, :desc, :author)';
db.query(sql, {
params:{
title:title,
desc:description,
author:author
}
}).then(function(results){
res.redirect('/topic/'+encodeURIComponent(results[0]['@rid']));
});
fs.writeFile('data/'+title, description, function(err){
if(err){
console.log(err);
res.status(500).send('Internal Server Error');
}
res.redirect('/topic/'+title);
})
})
app.get(['/topic', '/topic/:id'], function(req, res){
var sql = 'SELECT FROM topic';
db.query(sql).then(function(topics){
var sql = 'SELECT FROM topic WHERE @rid=:rid';
var id = req.params.id;
if(id){db.query(sql, {params:{rid:id}}).then(function(topic){
res.render('view' , {topics:topics, topic:topic[0]});
})} else {
res.render('view', {topics:topics});
}
})
});
app.listen(3000, function(){
console.log('Connected, 3000 port!');
})