Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f81020a

Browse files
committedJan 11, 2020
Update dependencies, fix mysql 8.0, add docker
1 parent 5e033f3 commit f81020a

File tree

10 files changed

+577
-38
lines changed

10 files changed

+577
-38
lines changed
 

‎.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
package-lock.json
2+
3+
data/db

‎README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
11
# Node.js-Express-With-MySQL
2-
This is a demo for node.je Express (ejs) with mysql
3-
 
4-
 
2+
This is a demo for node.je Express (ejs) with mysql 8.0
3+
54
## Blog
65
[Node.js - MySQL](https://dotblogs.com.tw/explooosion/2016/07/18/010601)
7-
 
8-
 
9-
 
106

11-
  
127
## Clone
13-
```bash
8+
9+
```sh
1410
git clone https://github.com/explooosion/Node.js-Express-With-MySQL.git
1511
```
16-
 
12+
1713
## Into Project
18-
```bash
14+
15+
```sh
1916
cd Node.js-Express-With-MySQL
2017
```
21-
 
18+
2219
## Install
23-
```bash
20+
21+
```sh
2422
npm install
2523
```
26-
 
24+
25+
## Start Mysql
26+
27+
Using docker-compose to create mysql container
28+
29+
```sh
30+
yarn db
31+
```
32+
33+
## Initial Database
34+
35+
Execute sql to initial table and fix auth for v8.0 in nodejs.
36+
37+
1. account.sql
38+
2. fix-mysql8-auth.sql
39+
40+
## Start Website
41+
42+
```sh
43+
yarn start
44+
```

‎app.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ var mysql = require("mysql");
1414
var con = mysql.createConnection({
1515
host: "localhost",
1616
user: "root",
17-
password: "",
18-
database: "test"
17+
password: "123456",
18+
database: "test",
1919
});
2020

21-
con.connect(function(err) {
21+
con.connect(function (err) {
2222
if (err) {
23-
console.log('connecting error');
23+
console.log('connecting error', err);
2424
return;
2525
}
2626
console.log('connecting success');
@@ -41,7 +41,7 @@ app.use(cookieParser());
4141
app.use(express.static(path.join(__dirname, 'public')));
4242

4343
// db state
44-
app.use(function(req, res, next) {
44+
app.use(function (req, res, next) {
4545
req.con = con;
4646
next();
4747
});
@@ -51,7 +51,7 @@ app.use('/users', users);
5151

5252

5353
// catch 404 and forward to error handler
54-
app.use(function(req, res, next) {
54+
app.use(function (req, res, next) {
5555
var err = new Error('Not Found');
5656
err.status = 404;
5757
next(err);
@@ -62,7 +62,7 @@ app.use(function(req, res, next) {
6262
// development error handler
6363
// will print stacktrace
6464
if (app.get('env') === 'development') {
65-
app.use(function(err, req, res, next) {
65+
app.use(function (err, req, res, next) {
6666
res.status(err.status || 500);
6767
res.render('error', {
6868
message: err.message,
@@ -73,7 +73,7 @@ if (app.get('env') === 'development') {
7373

7474
// production error handler
7575
// no stacktraces leaked to user
76-
app.use(function(err, req, res, next) {
76+
app.use(function (err, req, res, next) {
7777
res.status(err.status || 500);
7878
res.render('error', {
7979
message: err.message,

‎docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '2'
2+
3+
services:
4+
mysql:
5+
image: mysql:8
6+
container_name: mysql
7+
volumes:
8+
- ./data/db:/var/lib/mysql
9+
restart: always
10+
environment:
11+
- MYSQL_DATABASE=test
12+
- MYSQL_USER=user
13+
- MYSQL_PASSWORD=123456
14+
- MYSQL_ROOT_PASSWORD=123456
15+
ports:
16+
- 3306:3306

‎fix-mysql8-auth.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
2+
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
3+
-- or
4+
ALTER USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
5+
-- then
6+
FLUSH PRIVILEGES;

‎package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6-
"start": "node ./bin/www"
6+
"start": "node ./bin/www",
7+
"db": "docker-compose up --build -d"
78
},
89
"dependencies": {
9-
"body-parser": "~1.15.1",
10-
"cookie-parser": "~1.4.3",
11-
"debug": "~2.2.0",
12-
"ejs": "~2.4.1",
13-
"express": "~4.13.4",
14-
"morgan": "~1.7.0",
15-
"serve-favicon": "~2.3.0",
16-
"mysql": "~2.11.1"
10+
"body-parser": "~1.19.0",
11+
"cookie-parser": "~1.4.4",
12+
"debug": "~4.1.1",
13+
"ejs": "~3.0.1",
14+
"express": "~4.17.1",
15+
"morgan": "~1.9.1",
16+
"mysql": "2.17.1",
17+
"serve-favicon": "~2.5.0"
1718
}
1819
}

‎views/index.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<body>
3030
<h1>Account - List</h1>
3131
<div class="search">
32-
<label>UserId</label>
32+
<label>UserId:</label>
3333
<input type="text" name="suserid" value="<%=user %>" placeholder="input the userid">
3434
<input type="button" name="sSearch" value="Search" class="btn" onclick="Search();">
3535
</div>

‎views/userAdd.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
<form name="addform" action="userAdd" method="post" accept-charset="utf-8" onSubmit="return Check();">
2727
<h1>Account - Add</h1>
2828
<div class="row">
29-
<label>userid</label>
29+
<label>userid:</label>
3030
<input type="text" name="userid" placeholder="userid" />
3131
</div>
3232
<div class="row">
33-
<label>password</label>
33+
<label>password:</label>
3434
<input type="password" name="password" placeholder="password">
3535
</div>
3636
<div class="row">
37-
<label>email</label>
37+
<label>email:</label>
3838
<input type="text" name="email" placeholder="email">
3939
</div>
4040
<div class="row">

‎views/userEdit.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
<h1>Account - Edit</h1>
2828
<input type="hidden" name="id" value="<%=data[0].id %>" />
2929
<div class="row">
30-
<label>userid</label>
30+
<label>userid:</label>
3131
<input type="text" name="userid" placeholder="userid" value="<%=data[0].userid %>" readonly />
3232
</div>
3333
<div class="row">
34-
<label>password</label>
34+
<label>password:</label>
3535
<input type="text" name="password" placeholder="password" value="<%=data[0].password %>" />
3636
</div>
3737
<div class="row">
38-
<label>email</label>
38+
<label>email:</label>
3939
<input type="text" name="email" placeholder="email" value="<%=data[0].email %>">
4040
</div>
4141
<div class="row">

‎yarn.lock

Lines changed: 497 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
This repository has been archived.