express+mysqle

var mysql=require('mysql');
var connection=mysql.createConnection({
    host:'',//地址
    port:'',//端口号
    user:'',//用户名
    password:'',//密码
    database:'',//表名
    connectionLimit:100//最大连接数
});
connection.connect();//链接数据库
// 断开数据库--程序结束执行
// connection.end(function(err){
//     console.log(err);
// })
var express = require('express') ;
var app = express();
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
    else  next();
});
/*
获取数据
*/
app.get('/node/getItem',function(req,res){
    var sql='SELECT * FROM test_db';
    connection.query(sql, function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',error.message);
            return;
        }
        // var results=results[0];
        // console.log(results);
        var data={
            code:0,
            datas:results
        }
        res.send(data);
    });
})
/*
存数据
*/
app.get('/node/setItem',function(req,res){
    var addSql='INSERT INTO test_db (H,W,time) VALUES(?,?,?)';
    var addParams=[];
    for(var k in req.query){
        addParams.push(req.query[k])
    }
    var sql='SELECT * FROM test_db';
    connection.query(sql, function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',error.message);
            return;
        }
        for(var i=0;i<results.length;i++){
            if(results[i].time==req.query.time){
                res.send({
                    code:-1,
                    msg:'日期不能重复'
                });
                return;
            }else{

            }
        }
        connection.query(addSql,addParams, function (error, results) {
            if(error){
                console.log('[ADD ERROR] - ',error.message);
                return;
            }
            console.log(results);
            res.send({
                code:0
            });
            return;

        });
    });



})
/*
更新数据
*/
app.get('/node/undateItem',function(req,res){
    console.log(req.query);
    var updateSql='UPDATE test_db SET H=?,W=? WHERE time=?';
    var addParams=[];
    for(var k in req.query){
        addParams.push(req.query[k])
    }
    console.log(addParams);
    connection.query(updateSql,addParams, function (error, results) {
        if(error){
            console.log('[[UPDATE ERROR]] - ',error.message);
            return;
        }
        // console.log(results);
        res.send({
            code:0
        });

    });
})
var server = app.listen(80,function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log('服务端口',host,port);
})

这个demo并没有设置容错机制

posted @ 2017-09-20 14:18  地铁程序员  阅读(211)  评论(0编辑  收藏  举报