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);
网友评论