Skip to content

Instantly share code, notes, and snippets.

@nanha
Created February 12, 2013 08:19
Show Gist options
  • Save nanha/4760935 to your computer and use it in GitHub Desktop.
Save nanha/4760935 to your computer and use it in GitHub Desktop.
Da Young Shin 님에게.. 현재 님 소스에는 에러가 발생할 시에, 이를 화면에 표시해주는 if condition 처리가 되어 있지 않습니다. 1개의 예를 들어드릴께요.
/*
* GET home page.
*/
var mysql = require('mysql');
exports.index = function(req, res){
var client = mysql.createConnection({
host : '127.0.0.1',
user : '.....',
password : '.....',
database : 'Playground'
});
//
// 더 정확하게 connect 처리를 하고 싶을 때에는 아래같이 하면 되요.
// 만약 에러가 발생하면, res 객체에 전달하면 되고요.
//
client.connect(function(err) {
res.render('index', { error: err.toString(), title: 'Express' });
});
//
// 존재하지 않는 테이블에 쿼리
// 만약 위에 데이타베이스가 존재하지 않을 경우에도
// 아래 쿼리는 요청되지 않기 때문에, 에러가 발생합니다.
//
client.query('SELECT * FROM user2', function(err, results) {
if (err) {
//
// 에러가 발생하면, 아래와 같이 err 내용을 랜더링 하셔야 합니다.
// 현재는 console 만 출력해놨죠.
// node 는 비동기 이기 때문에
//
res.render('index', { error: err.toString(), title: 'Express' });
} else {
res.render('index', { error: 'foo', title: 'Express' });
}
});
};
# views/index.jade 의 내용이에요.
# error 내용이 있을 경우에만 보여지도록 추가하면 되요.
extends layout
block content
h1= title
h1= error
p Welcome to #{title}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment