-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 51d3903
Showing
10 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.idea | ||
.DS_Store | ||
node_modules/ | ||
dist/ | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
const express = require('express') | ||
const app = express() | ||
|
||
const query = require('./utils/utils') | ||
// 跨域设置 | ||
app.all('*', function (req, res, next) { | ||
res.header("Access-Control-Allow-Credentials", true) | ||
res.header("Access-Control-Allow-Origin", "*") | ||
res.header("Access-Control-Allow-Headers", "X-Requested-With") | ||
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS") | ||
res.header("X-Powered-By", ' 3.2.1') | ||
res.header("Content-Type", "application/json;charset=utf-8") | ||
next() | ||
}) | ||
|
||
app.get('/', function (req, res) { | ||
res.send('hello express') | ||
}) | ||
|
||
app.get('/book', require('./router/book')) | ||
|
||
app.get('/booklist', require('./router/booklist')) | ||
|
||
app.get('/titles', require('./router/booktitles')) | ||
|
||
app.get('/type', require('./router/type')) | ||
|
||
const port = process.env.PORT || 3333 | ||
|
||
app.listen(3333, () => { | ||
console.log(`server running @${port}`) | ||
}) | ||
|
||
|
||
/*function pattern(num) { | ||
for (var i = 0; i < num; i++) { | ||
let a = `1${'*'.repeat(i)}${i + 1}` | ||
console.log(i < 1 ? a.replace('11', '1') : a) | ||
} | ||
} | ||
pattern(10) | ||
function aaa(num) { | ||
for (var i = 0; i < num; i++) { | ||
let a = `${'1'.padEnd(i + 1, '*')}${i + 1}` | ||
console.log(i < 1 ? a.replace('11', '1') : a) | ||
} | ||
} | ||
function star(tel) { | ||
return `${tel.substr(0,3)}${'*'.repeat(4)}${tel.substr(7)}` | ||
}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Created by 12 on 2017/7/4. | ||
*/ | ||
var mysql = require('mysql') | ||
|
||
const pool = mysql.createPool({ | ||
host: 'localhost', | ||
user: 'root', | ||
password: '460520', | ||
database: 'book', | ||
port: 3306 | ||
}) | ||
|
||
for (var i = 1; i <= 200; i++) { | ||
(function (i) { | ||
var sql = ` | ||
CREATE TABLE IF NOT EXISTS \`book${i}\`( | ||
\`id\` decimal(10,0) NOT NULL, | ||
\`err\` decimal(3,0) DEFAULT NULL, | ||
\`bookName\` varchar(100) DEFAULT NULL, | ||
\`title\` varchar(100) DEFAULT NULL, | ||
\`content\` mediumtext, | ||
PRIMARY KEY (\`id\`) | ||
) ENGINE=MyISAM DEFAULT CHARSET=utf8; | ||
` | ||
pool.query(sql, function (error, results, fields) { | ||
// And done with the connection. | ||
console.log('create ' + i + ' success!') | ||
// Handle error after the release. | ||
if (error) throw error | ||
// Don't use the connection here, it has been returned to the pool. | ||
}) | ||
})(i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "node-mysql", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"async": "^2.5.0", | ||
"express": "^4.15.3", | ||
"mysql": "^2.13.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
const query = require('../utils/utils') | ||
|
||
const router = (req, res) => { | ||
const book = req.query.book | ||
const id = req.query.id | ||
|
||
|
||
query(`select * from book${book} where id=${id};`, [1], (err, results, fields) => { | ||
if (err) throw err | ||
res.send(results[0]) | ||
}) | ||
} | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
const query = require('../utils/utils'); | ||
|
||
const router = (req, res) => { | ||
const id = req.query.id || 0 | ||
query(id ? `select * from booklist where id=${id};` : `select * from booklist;`, [1], (err, results, fields) => { | ||
if (err) throw err | ||
id ? res.send(results[0]) : res.send(results) | ||
}) | ||
} | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
const query = require('../utils/utils'); | ||
|
||
const router = (req, res) => { | ||
const id = req.query.id | ||
|
||
query(`select * from booktitles where id=${id};`, [1], (err, results, fields) => { | ||
if (err) throw err | ||
res.send(results[0]) | ||
}) | ||
} | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
const query = require('../utils/utils') | ||
|
||
const router = (req, res) => { | ||
const book = req.query.book | ||
const id = req.query.id | ||
|
||
query(`select * from book${book} where id=${id};`, [1], (err, results, fields) => { | ||
if (err) throw err | ||
res.send(results[0]) | ||
}) | ||
} | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
const query = require('../utils/utils') | ||
|
||
function getType(type) { | ||
if (type == 1) { | ||
return '玄幻' | ||
} else if (type == 2) { | ||
return '修真' | ||
} else if (type == 3) { | ||
return '都市' | ||
} else if (type == 4) { | ||
return '历史' | ||
} else if (type == 5) { | ||
return '网游' | ||
} | ||
} | ||
|
||
//玄幻1 修真2 都市3 历史4 网游5 | ||
const router = (req, res) => { | ||
let type = getType(req.query.type) | ||
//坑1 sql语句中要加上引号 | ||
query(`select * from booklist where type='${type}'`, [1], (err, results, fields) => { | ||
if (err) throw err | ||
res.send(results) | ||
}) | ||
} | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Created by 12 on 2017/7/3. | ||
*/ | ||
let mysql = require('mysql') | ||
const pool = mysql.createPool({ | ||
host: 'localhost', | ||
user: 'root', | ||
password: '460520', | ||
database: 'book', | ||
port: 3306 | ||
}) | ||
|
||
const query = function (sql, option, callback) { | ||
pool.getConnection(function (err, connection) { | ||
// Use the connection | ||
connection.query(sql, option, function (error, results, fields) { | ||
// And done with the connection. | ||
connection.release() | ||
|
||
// Handle error after the release. | ||
if (error) throw error | ||
callback(err, results, fields) | ||
// Don't use the connection here, it has been returned to the pool. | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = query |