node + mysql + express

var mysql = require('mysql');

var express = require('express');

var bodyParser = require("body-parser");

var app = express();

var server = app.listen(8000,function(){

console.log('88aaaa')

})

app.get('/user/list',function(req,res){

res.header("Access-Control-Allow-Origin", "*");//前端请求跨域,请求头设置

var connection = mysql.createConnection({   

  host : 'localhost',     

  user : 'root',             

  password : 'root',     

  port: 3306,                 

  database: 'user',

});

connection.connect();

var sql = 'select * from tasks where subject="任务-1"';

connection.query(sql,function(err,result){

if(err){

console.log(err);

return;

}

var obj = {};

obj.success = true;

obj.data = result;

var json = JSON.stringify(obj)

res.send(json)

})

connection.end();

})

app.use(bodyParser.urlencoded({ extended: false }));

app.post('/user/detail',function(req,res){

    console.log(req.body.subject)

    res.header("Access-Control-Allow-Origin", "*");

    var connection = mysql.createConnection({   

       host : 'localhost',     

       user : 'root',             

       password : 'qiwei123',     

       port: 3306,                 

       database: 'user',

    });

    connection.connect();

    var sql = 'select subject,task_id,start_date,end_date from tasks where subject='+connection.escape(req.body.subject);//connection.escape(req.body.subject)    防止sql注入

// var sql = 'select * from tasks where subject='+connection.escape(req.body.subject);

    connection.query(sql,function(err,result){

    if(err){

        console.log(err);

        return;

    }

        var obj = {};

        obj.success = true;

        obj.data = result;

        var json = JSON.stringify(obj)

        res.send(json)

    })

    connection.end();

})

你可能感兴趣的:(node + mysql + express)