//最上面的参数
var URL = require('url');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.render('index');
});
//数据库
var table = 'user';var mydb = {
host : '127.0.0.1',
user : 'root',
password : 'root',
port : '3306',
database: 'test',
};
var con = mysql.createConnection(mydb);
con.connect();
//添加
router.get('/addUser',function(req,res,next){
var params = URL.parse(req.url, true).query;
var sql = 'INSERT INTO '+table+' VALUES(null,?,?)';
var param = [params.name,params.pwd];
con.query(sql,param,function(err,result){
res.send(JSON.stringify(result));
});
});
//查询
router.get('/show',function(req,res,next){
var sql = 'SELECT * FROM '+table;
con.query(sql,function(err,result){
res.send(JSON.stringify(result));
})
})
//删除
router.get('/del', function(req, res, next) {
var params = URL.parse(req.url, true).query;
var id = params.id;
var sql = 'DELETE FROM '+table+' WHERE id=?';
con.query(sql,[id],function(err,result){
if(result)
{
res.render('form');
}else{
// res.json(result);
}
})
});
//修改接值
router.get('/save', function(req, res, next) {
var params = URL.parse(req.url, true).query;
var id = params.id;
var sql = 'SELECT * FROM '+table+' WHERE id=?';
//con.query(sql,[id],function(err,result){
con.query(sql,[id],function(err,result){
if(err)
{
res.json(err.message);
}
res.send(JSON.stringify(result));
})
})
//修改接值
router.get('/update', function(req, res, next) {
var params = URL.parse(req.url, true).query;
var id = params.id;
var name = params.name;
var pwd = params.pwd;
var sql = 'UPDATE '+table+' SET `name`=? , `pwd`=? WHERE id=?';
con.query(sql,[name,pwd,id],function(err,result){
if(err)
{
res.json(err.message);
}
var success=1;
res.json(success);
})
})
//登录提交
router.get('/loginDo', function(req, res, next) {
var params = URL.parse(req.url, true).query;
var name = params.name;
var pwd = params.pwd;
var sql = 'SELECT * FROM '+table+' WHERE name=? AND pwd=?';
//con.query(sql,[id],function(err,result){
con.query(sql,[name,pwd],function(err,result){
if(err)
{
res.json(err.message);
}
//res.send(JSON.stringify(result));
var success=1;
res.json(success);
})
})