From e29956222073034f8f33f6cb0572c61d26d58d59 Mon Sep 17 00:00:00 2001 From: luchenqun Date: Mon, 13 Feb 2017 09:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/db.js | 47 ++++++++++++++++++++++++++++++++++------------- routes/api.js | 1 - 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/database/db.js b/database/db.js index 0d62c9a..e7e09b0 100644 --- a/database/db.js +++ b/database/db.js @@ -1,12 +1,35 @@ var mysql = require('mysql'); -var client = mysql.createConnection({ +var dbConfig = { host: '127.0.0.1', user: 'test', // mysql的账号 password: '123456', // mysql 的密码 database: 'mybookmarks', multipleStatements: true, port: 3306 -}); +}; +var client = {} + +function handleDisconnect() { + client = mysql.createConnection(dbConfig); + + client.connect(function(err) { // The server is either down + if (err) { // or restarting (takes a while sometimes). + console.log('error when connecting to db:', err); + setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect, + } // to avoid a hot loop, and to allow our node script to + }); // process asynchronous requests in the meantime. + // If you're also serving http, display a 503 error. + client.on('error', function(err) { + console.log('db error', err); + if (err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually + handleDisconnect(); // lost due to either server restart, or a + } else { // connnection idle timeout (the wait_timeout + throw err; // server variable configures this) + } + }); +} + +handleDisconnect(); Date.prototype.format = function(fmt) { //author: meizz var o = { @@ -24,22 +47,20 @@ Date.prototype.format = function(fmt) { //author: meizz return fmt; } -client.connect(); - // select 最多返回一行的话,返回对象,否则返回数组 // insert 返回关键字 // update delete 返回影响的行数 var db = { -} -// var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun'"; -// client.query(sql, (err, result) => { -// if (err) { -// console.log(err); -// } else { -// console.log(result); -// } -// }); + } + // var sql = "SELECT * FROM `users` WHERE `username` = 'luchenqun'"; + // client.query(sql, (err, result) => { + // if (err) { + // console.log(err); + // } else { + // console.log(result); + // } + // }); db.addBookmark = function(user_id, bookmark) { var insertSql = "INSERT INTO `bookmarks` (`user_id`, `title`, `description`, `url`, `public`, `click_count`) VALUES ('" + user_id + "', '" + bookmark.title + "', '" + bookmark.description + "', '" + bookmark.url + "', '" + bookmark.public + "', '1')"; diff --git a/routes/api.js b/routes/api.js index 0b4d6ef..e9f7c3d 100644 --- a/routes/api.js +++ b/routes/api.js @@ -482,7 +482,6 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) { var file = req.file; res.json(file); parseHtml(file.path, function(data) { - console.log(data); var bookmarks = data.bookmarks; var tagsName = data.tags;