├── .gitignore
├── upgrade.log
├── bin
└── http-mysql-server
├── package.json
├── README.md
├── view.js
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/upgrade.log:
--------------------------------------------------------------------------------
1 | 服务启动成功, 请访问 “http://127.0.0.1:5555"
2 |
3 | 服务启动成功, 请访问 “http://127.0.0.1:555"
4 |
5 |
--------------------------------------------------------------------------------
/bin/http-mysql-server:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 | const path = require('path')
3 | const argv = require('yargs').argv;
4 | let configPath = argv.config;
5 | let config;
6 |
7 | if(configPath){
8 | configPath = path.resolve(process.cwd(), configPath);
9 | config = require(configPath)
10 | }else{
11 | config = argv;
12 | }
13 |
14 |
15 | require('../index')(config)
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "http-mysql-server",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "bin": {
10 | "http-mysql-server": "./bin/http-mysql-server"
11 | },
12 | "author": "",
13 | "license": "ISC",
14 | "dependencies": {
15 | "koa": "^2.5.1",
16 | "koa-body": "^2.5.0",
17 | "mysql": "^2.15.0",
18 | "yargs": "^11.0.0"
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## 安装
2 |
3 | ### 1.使用 npm 仓库安装
4 | ```
5 | npm install -g http-mysql-server
6 | ```
7 | ### 2.使用源码 安装
8 | ```bash
9 | #a.下载源码
10 | #b.进入源码包、安装依赖
11 | npm install
12 | #c. 链接脚本
13 | npm link
14 |
15 | ```
16 |
17 | ## 启动服务器
18 |
19 | mysql的用户名、密码、数据库名、数据库的地址、端口、http服务的端口 ,下面罗列的均为默认值,可以不填
20 |
21 | ```bash
22 | http-mysql-server --user root --password 123456 --host 127.0.0.1 --database test --port 3306 --http_port 5555
23 | #如只指定 数据库端口 其他均用默认值
24 | http-mysql-server --port 3306
25 | ```
26 |
27 | ## 使用方法
28 |
29 | 启动服务器后,可对该 HTTP 服务器发起 `POST` 请求,假设服务器访问地址为 `http://127.0.0.1:5555`,那么请求地址为 `http://127.0.0.1:555/api/query`
30 |
31 | 请求参数如下:
32 |
33 | |Name|Descrption|
34 | |------|-------|
35 | |sql|需要查询的 sql 语句|
36 |
37 | ## 示例
38 |
39 | 
40 |
--------------------------------------------------------------------------------
/view.js:
--------------------------------------------------------------------------------
1 | module.exports = function(tableHtml, port) {
2 | return `
3 |
10 |
使用方法
11 |
12 |
13 | 对Api “http://127.0.0.1:${port}/api/query” 发起 "post" 请求,参数如下:
14 |
15 |
16 |
17 |
18 | | 参数名 |
19 | 描述 |
20 |
21 |
22 | | sql |
23 | 需要查询的 sql 语句 |
24 |
25 |
26 |
27 | Jquery 示例
28 |
29 | $.post('http://127.0.0.1:${port}/api/query', {sql: 'show tables'}, function(res){
30 | console.log(res)
31 | })
32 |
33 | 数据表如下
34 |
37 | `;
38 | };
39 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | const koa = require("koa");
2 | const koaBody = require("koa-body");
3 | const mysql = require("mysql");
4 | const argv = require("yargs").argv;
5 |
6 | module.exports = function(options) {
7 | function query(pool, sql) {
8 | return new Promise((resolve, reject) => {
9 | pool.getConnection(function(err, connection) {
10 | if(err){
11 | err.message += '数据库连接失败'
12 | return reject(err)
13 | }
14 | connection.query(sql, function(error, results, fields) {
15 | connection.release();
16 | if (error) reject(error);
17 | resolve({
18 | results,
19 | fields
20 | });
21 | });
22 | });
23 | });
24 | }
25 |
26 | /**
27 | * config Object
28 | * host: "localhost",
29 | * user: "root",
30 | * password: "12345678",
31 | * database: "test"
32 | *
33 | * 详细 config 配置查看 https://github.com/mysqljs/mysql#connection-options
34 | */
35 |
36 | config = {
37 | connectionLimit: 10,
38 | host: options.host || 'localhost',
39 | port: options.port || 3306,
40 | user: options.user || 'root',
41 | database: options.database || 'test',
42 | password: (options.password + '') || 'root'
43 | }
44 |
45 | const port = options.http_port || "5555";
46 |
47 | const pool = mysql.createPool(config);
48 |
49 | const app = new koa();
50 |
51 | app.use(
52 | koaBody({
53 | multipart: true
54 | })
55 | );
56 |
57 | app.use((ctx, next)=>{
58 | return next()
59 | })
60 |
61 | app.use(async (ctx, next) => {
62 | if (ctx.path.indexOf("/api/query") === 0) {
63 | let params = ctx.request.body;
64 | let sql = params.sql;
65 | try {
66 | let data = await query(pool, sql);
67 | return (ctx.body = data.results);
68 | } catch (err) {
69 | return (ctx.body = {
70 | errcode: 400,
71 | message: err.message
72 | });
73 | }
74 | }
75 | return await next();
76 | });
77 |
78 | app.use(async ctx => {
79 | let result
80 | try{
81 | result = await query(pool, "show tables");
82 | }catch(err){
83 | return ctx.body = err.message
84 | }
85 |
86 | let tables = result.results.map(item => item["Tables_in_"+config.database]);
87 | let tableHtml = [];
88 | tables.forEach((item, index) =>
89 | tableHtml.push(
90 | "| " + (index + 1) + " | " + item + " |
"
91 | )
92 | );
93 |
94 | ctx.body = require('./view')(tableHtml, port);
95 | });
96 |
97 |
98 | app.listen(port, function() {
99 | console.log("服务启动成功, 请访问 “http://127.0.0.1:" + port + '"\n');
100 | });
101 | app.on('error', async (err, ctx, next) => {
102 |
103 | // TODO logger errStack
104 | console.error(err.message);
105 |
106 | });
107 | };
108 |
109 |
110 |
111 | var fs = require('fs')
112 | var util = require('util')
113 |
114 | var logPath = 'upgrade.log'
115 | var logFile = fs.createWriteStream(logPath, { flags: 'a' })
116 |
117 | console.log = function() {
118 | logFile.write(util.format.apply(null, arguments) + '\n')
119 | process.stdout.write(util.format.apply(null, arguments) + '\n')
120 | }
121 |
122 | console.error = function() {
123 | logFile.write(util.format.apply(null, arguments) + '\n')
124 | process.stderr.write(util.format.apply(null, arguments) + '\n')
125 | }
--------------------------------------------------------------------------------