美文网首页
node + mysql + express

node + mysql + express

作者: 一本正经地胡说八道_bbeb | 来源:发表于2018-07-11 11:34 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/jyawxxtx.html