dev/데이터베이스
orient db 를 이용한 코드 중간단계.
jeongsu
2018. 1. 16. 20:18
/* * express 모듈 객체 생성 */ var express = require('express'); var app = express(); /* * orient db 객체 생성 및 연결 */ var Oriento = require('orientjs'); var server = Oriento({ host: 'localhost', port: 2424, username: 'root', password: '자신의 비밀번호' }); var db=server.use('o2'); /* * pug를 위한 설정 */ app.set('views','./views_orient'); app.set('view engine','pug'); /* * 요걸 해줘야 post 방식으로 전달된 내용들을 파싱 할수 있다. */ var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); 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){ var rid = encodeURIComponent(results[0]['@rid']); res.redirect('/topic/'+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 sql = 'SELECT FROM topic where @rid=:rid'; db.query(sql,{params:{rid:id}}).then(function(topic){ console.log(topic[0]); res.render('view',{topics:topics,topic:topic[0]}); }); } else{ res.render('view',{topics:topics}); } }); }); app.listen(3000,function(){ console.log('connect 3000 port'); });
흠.. 이고잉님꺼 계속 따라하고 있다 중간단계다.. 조금 쉬자..