美文网首页
跨域转掉以及增删改查

跨域转掉以及增删改查

作者: 皮卡乒乓 | 来源:发表于2018-02-09 10:44 被阅读0次
var express = require("express");
var request = require("request");
var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'mysql',
    port:3307
});
connection.connect();

var app = new express();

app.get("/queryExpress", function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");

    var type = req.query.type;
    var postId = req.query.postId;

    request("http://www.kuaidi100.com/query?type=" + type + "&postid=" + postId + "", function (error, response) {

        res.send(response.body);

        var addSql = 'INSERT INTO express(id,type,postId,express) VALUES(0,?,?,?)';
        var addSqlParams = [type, postId, response.body];
        connection.query(addSql, addSqlParams, function (err, result) {
            if (err) {
                console.log('[INSERT ERROR] - ', err.message);
                return;
            }
            console.log('----INSERT----');
        });
    })
})

app.get("/queryExitExpress", function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    var type = req.query.type;

    connection.query({
        sql: 'SELECT * FROM express WHERE `type` = ?',
        timeout: 40000, 
        values: [type]
    }, function (err, result,fields) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }
        console.log('---SELECT---');
        res.send(result);
    });
})

app.get("/delExitExpressByType", function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    var type = req.query.type;

    connection.query({
        sql: 'DELETE FROM express WHERE `type` = ?',
        timeout: 40000, 
        values: [type]
    }, function (err, result,fields) {
        if (err) {
            console.log('[DELETE  ERROR] - ', err.message);
            return;
        }
        console.log('---DELETE---');
        res.send("删除成功");
    });
})

app.listen(3000);

相关文章

网友评论

      本文标题:跨域转掉以及增删改查

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