Skip to content

Commit ab6185f

Browse files
author
William Wang
committed
init
0 parents  commit ab6185f

File tree

12 files changed

+451
-0
lines changed

12 files changed

+451
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

app.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
8+
var routes = require('./routes/index');
9+
var users = require('./routes/users');
10+
11+
var app = express();
12+
13+
// view engine setup
14+
app.set('views', path.join(__dirname, 'views'));
15+
app.set('view engine', 'ejs');
16+
17+
// uncomment after placing your favicon in /public
18+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
19+
app.use(logger('dev'));
20+
app.use(bodyParser.json());
21+
app.use(bodyParser.urlencoded({ extended: false }));
22+
app.use(cookieParser());
23+
app.use(express.static(path.join(__dirname, 'public')));
24+
25+
app.use('/', routes);
26+
app.use('/users', users);
27+
28+
// catch 404 and forward to error handler
29+
app.use(function(req, res, next) {
30+
var err = new Error('Not Found');
31+
err.status = 404;
32+
next(err);
33+
});
34+
35+
// error handlers
36+
37+
// development error handler
38+
// will print stacktrace
39+
if (app.get('env') === 'development') {
40+
app.use(function(err, req, res, next) {
41+
res.status(err.status || 500);
42+
res.render('error', {
43+
message: err.message,
44+
error: err
45+
});
46+
});
47+
}
48+
49+
// production error handler
50+
// no stacktraces leaked to user
51+
app.use(function(err, req, res, next) {
52+
res.status(err.status || 500);
53+
res.render('error', {
54+
message: err.message,
55+
error: {}
56+
});
57+
});
58+
59+
60+
module.exports = app;

bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('blog:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

blog.sql

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.4.10
3+
-- http://www.phpmyadmin.net
4+
--
5+
-- 主機: localhost
6+
-- 產生時間: 2016 年 04 月 14 日 07:13
7+
-- 伺服器版本: 5.5.42
8+
-- PHP 版本: 7.0.0
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET time_zone = "+00:00";
12+
13+
14+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
15+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
16+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
17+
/*!40101 SET NAMES utf8mb4 */;
18+
19+
--
20+
-- 資料庫: `blog`
21+
--
22+
23+
-- --------------------------------------------------------
24+
25+
--
26+
-- 資料表結構 `article`
27+
--
28+
29+
CREATE TABLE `article` (
30+
`id` int(11) unsigned NOT NULL,
31+
`title` varchar(20) NOT NULL DEFAULT '',
32+
`content` text NOT NULL,
33+
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
34+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
35+
36+
-- --------------------------------------------------------
37+
38+
--
39+
-- 資料表結構 `article_tag`
40+
--
41+
42+
CREATE TABLE `article_tag` (
43+
`id` int(11) unsigned NOT NULL,
44+
`article_id` int(11) unsigned NOT NULL,
45+
`tag_id` int(11) unsigned NOT NULL
46+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
47+
48+
-- --------------------------------------------------------
49+
50+
--
51+
-- 資料表結構 `comment`
52+
--
53+
54+
CREATE TABLE `comment` (
55+
`id` int(11) unsigned NOT NULL,
56+
`member_id` int(11) unsigned NOT NULL,
57+
`article_id` int(11) unsigned NOT NULL,
58+
`content` text NOT NULL,
59+
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
60+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
61+
62+
-- --------------------------------------------------------
63+
64+
--
65+
-- 資料表結構 `member`
66+
--
67+
68+
CREATE TABLE `member` (
69+
`id` int(11) unsigned NOT NULL,
70+
`name` varchar(20) NOT NULL DEFAULT '',
71+
`account` varchar(60) NOT NULL DEFAULT '',
72+
`password` varchar(255) NOT NULL DEFAULT ''
73+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
74+
75+
-- --------------------------------------------------------
76+
77+
--
78+
-- 資料表結構 `tag`
79+
--
80+
81+
CREATE TABLE `tag` (
82+
`id` int(11) unsigned NOT NULL,
83+
`name` varchar(20) NOT NULL DEFAULT ''
84+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
85+
86+
--
87+
-- 已匯出資料表的索引
88+
--
89+
90+
--
91+
-- 資料表索引 `article`
92+
--
93+
ALTER TABLE `article`
94+
ADD PRIMARY KEY (`id`);
95+
96+
--
97+
-- 資料表索引 `article_tag`
98+
--
99+
ALTER TABLE `article_tag`
100+
ADD PRIMARY KEY (`id`),
101+
ADD KEY `arti` (`article_id`),
102+
ADD KEY `ta` (`tag_id`);
103+
104+
--
105+
-- 資料表索引 `comment`
106+
--
107+
ALTER TABLE `comment`
108+
ADD PRIMARY KEY (`id`),
109+
ADD KEY `member_id` (`member_id`),
110+
ADD KEY `article_id` (`article_id`);
111+
112+
--
113+
-- 資料表索引 `member`
114+
--
115+
ALTER TABLE `member`
116+
ADD PRIMARY KEY (`id`);
117+
118+
--
119+
-- 資料表索引 `tag`
120+
--
121+
ALTER TABLE `tag`
122+
ADD PRIMARY KEY (`id`);
123+
124+
--
125+
-- 在匯出的資料表使用 AUTO_INCREMENT
126+
--
127+
128+
--
129+
-- 使用資料表 AUTO_INCREMENT `article`
130+
--
131+
ALTER TABLE `article`
132+
MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;
133+
--
134+
-- 使用資料表 AUTO_INCREMENT `article_tag`
135+
--
136+
ALTER TABLE `article_tag`
137+
MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;
138+
--
139+
-- 使用資料表 AUTO_INCREMENT `comment`
140+
--
141+
ALTER TABLE `comment`
142+
MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;
143+
--
144+
-- 使用資料表 AUTO_INCREMENT `member`
145+
--
146+
ALTER TABLE `member`
147+
MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;
148+
--
149+
-- 使用資料表 AUTO_INCREMENT `tag`
150+
--
151+
ALTER TABLE `tag`
152+
MODIFY `id` int(11) unsigned NOT NULL AUTO_INCREMENT;
153+
--
154+
-- 已匯出資料表的限制(Constraint)
155+
--
156+
157+
--
158+
-- 資料表的 Constraints `article_tag`
159+
--
160+
ALTER TABLE `article_tag`
161+
ADD CONSTRAINT `IDX_01` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
162+
ADD CONSTRAINT `IDX_02` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;
163+
164+
--
165+
-- 資料表的 Constraints `comment`
166+
--
167+
ALTER TABLE `comment`
168+
ADD CONSTRAINT `IDX02` FOREIGN KEY (`member_id`) REFERENCES `member` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
169+
ADD CONSTRAINT `IDX03` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;
170+
171+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
172+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
173+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

libs/db.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var mysql = require('mysql');
2+
var debug = require('debug')('MYSQL');
3+
4+
var pool = mysql.createPool(process.env.DB_PATH || 'mysql://root:root@localhost:8889/blog');
5+
6+
module.exports = pool;

libs/migrate.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var db = require('./db');
2+
var bcrypt = require('bcryptjs');
3+
var async = require('async');
4+
5+
var salt = "blog-password-salt-edu-nccu-soslab";
6+
7+
var memberList = [{
8+
name : "William1",
9+
password : "password1",
10+
account : "account1"
11+
},
12+
{
13+
name : "William2",
14+
password : "password2",
15+
account : "account2"
16+
},
17+
{
18+
name : "William3",
19+
password : "password3",
20+
account : "account3"
21+
}];
22+
23+
db.query("DROP DATABASE blog", function(err) {
24+
if(err) {
25+
console.log(err);
26+
} else {
27+
console.log("CLEAR");
28+
}
29+
});
30+
31+
// insertMember(memberList,function(err) {
32+
// if(err) {
33+
// console.log(err);
34+
// } else {
35+
// console.log("MEMBER INSERT DONE");
36+
// }
37+
// });
38+
39+
40+
function insertMember(memberList, cb) {
41+
async.each(memberList, function(member, callback) {
42+
db.query("INSERT INTO member (name, account, password) VALUES (?, ?, ?)",[
43+
member.name,
44+
member.account,
45+
bcrypt.hashSync(salt+member.password, 10)
46+
], function(err, result) {
47+
if(err) {
48+
callback(err);
49+
} else {
50+
callback(null);
51+
}
52+
});
53+
}, function(err) {
54+
if(err) {
55+
cb(err);
56+
} else {
57+
cb(null);
58+
}
59+
})
60+
};

0 commit comments

Comments
 (0)