赞
踩
下面的操作,我是通过phpStudy
和Navicat for MySQL
搭建起来的开发环境
我提供一个Navicat for MySQL
的安装步骤:https://blog.csdn.net/mossbaoo/article/details/88661851
先来看看目前MySQL的数据:
node连接MySQL,需要安装express
和mysql
模块
var express = require("express");
var mysql = require('mysql');
var app = express();
// 配置数据库信息
var connection = mysql.createConnection({
host: 'localhost', // 主机地址
// port: '3306', // 端口
user: 'root', // 用户名
password: 'root', // 密码
database: 'websites' // 数据库名
});
// 连接
connection.connect();
连接成功之后,就可以进行用户端与服务端的交互了,分为GET
和POST
它们的区别是:GET
用req.query
,POST
用req.body
GET
app.get("/getType", function (req, res) {
console.log(req.query);
var sql = 'select * from mo_type_list';
connection.query(sql , function (err, data) {
if (err) {
console.log(err);
} else {
var result = {
"status": "200",
"message": "success",
}
result.data = data;
res.end(JSON.stringify(result));
}
});
});
getType() {
let that = this;
wx.request({
url: 'http://localhost:3000/getType',
method: 'GET',
data: {},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success(res) {
console.log(res.data.data)
}
})
},
POST
body-parser
模块var express = require("express");
var mysql = require('mysql');
var bodyParser = require("body-parser"); // post需要
app.use(bodyParser.urlencoded({ extended: true })); // post需要
app.post("/addType", function (req, res) {
var params = [req.body.name];
var sql = "INSERT INTO mo_type_list(Id,name) VALUES(0,?)";
connection.query(sql, params, function (err, data) {
if (err) {
res.end('error')
} else {
var result = {
"status": "200",
"message": 'success',
}
res.end(JSON.stringify(result))
}
});
});
addType() {
let that = this;
wx.request({
url: 'http://localhost:3000/addType',
method: 'POST',
data: {
name: '人事部'
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success(res) {
console.log('success')
}
})
},
服务端的全部代码
var express = require("express");
var querystring = require('querystring');
var mysql = require('mysql');
// post需要
var bodyParser = require("body-parser");
var app = express();
// post需要
app.use(bodyParser.urlencoded({ extended: true }));
// 跨域
app.all('*', function (req, res, next) {
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();
});
// 配置数据库信息
var connection = mysql.createConnection({
host: 'localhost',
// port: '3306',
user: 'root',
password: 'root',
database: 'websites'
});
// 连接
connection.connect();
// 获取类型列表
app.get("/getType", function (req, res) {
var sql = 'select * from mo_type_list';
connection.query(sql , function (err, data) {
if (err) {
console.log(err);
} else {
var result = {
"status": "200",
"message": "success",
}
result.data = data;
res.end(JSON.stringify(result));
}
});
});
// 添加类型
app.post("/addType", function (req, res) {
var params = [req.body.name];
var sql = "INSERT INTO mo_type_list(Id,name) VALUES(0,?)";
connection.query(sql, params, function (err, data) {
if (err) {
res.end('error')
} else {
var result = {
"status": "200",
"message": 'success',
}
res.end(JSON.stringify(result))
}
});
});
app.listen(3000);
console.log('3000 running');
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。