【前端】nodejs实现多重查找,避免异步操作使返回数据为空;得到post请求数据并处理

nodejs安装post请求依赖:npm install express body-parser

const express = require('express');
var async = require('async');
const app = express();
//提供post请求支持
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

//数据库连接并启动连接池
const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',     // 改成你自己的密码
    database: 'zengyifan'    // 改成你的数据库名称
});
connection.connect();

// 解决跨域请求问题
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

/*----------------------------------------接口部分----------------------------------------*/

/**
* 实现多重查找并返回想要的结果,主要是async.map那个操作
*/
app.get('/getAllOrder', function (req, res) {
    var sql = 'select * from torder ORDER BY create_time ASC';
    //查询
    connection.query(sql, function (err, result) {
        if (err) {
            res.send(err.message);
            return;
        }
        async.map(result, function(item, callback) {
            var sql = 'select * from consumer_goods where order_id = ?';
            connection.query(sql, item.id, function(err, goods){
                item.goods = goods;
                console.log("item",item)
                callback(null, item);
            });
        }, function(err,results) {
            console.log("result",result)
            res.send(results);//把数据返回
        });
    });
});

/**
* 得到post请求并处理
*/
app.post('/upLoadBlog', function (req, res) {
    var title = req.body.title;
    var homeworkName = req.body.homeworkName;
    var homeworkId = req.body.homeworkId;
    var summary = req.body.summary;
    var content = req.body.content;
    var userId = req.body.userId;
    var userName = req.body.userName;
    var avatar = req.body.avatar;
    var sql = 'INSERT into blog\n' +
        '    (title,homework_name,homework_id,summary,content,createTime,user_id, user_name,avatar)\n' +
        '    values (?,?,?,?,?,now(),?,?,?)';
    var sqlParams = [title, homeworkName, homeworkId, summary, content, userId, userName, avatar];
    connection.query(sql, sqlParams, function (err, result) {
        res.send(result);
    });

});


/*----------------------------------------起服务----------------------------------------*/
var server = app.listen(8000, '127.0.0.1', function () {

    var host = server.address().address;
    var port = server.address().port;
    console.log("地址为 http://%s:%s", host, port);
});

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值