Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

node express服务端 post请求返回res.send()前需要先请求其他服务器的post,对数据进行整合返回到前端?

router/user

/**
 * Created by xg on 2017/4/4.
 */
"use strict";
// var request = require('request');
var express = require('express');
var router = express.Router();

//service
var user_service = require("../service/user");
//util
var res_format = require("../util/response_format");

// 该路由使用的中间件 timeLog
router.use(function timeLog(req, res, next) {
    console.log('Time: ', Date.now());
    next();
});
// 定义网站主页的路由
router.get('/', function (req, res, next) {
    // console.log(req);
    res.send(req.query || {});
});

// 用户注册
router.post('/sign', function (req, res, next) {
    console.log(req.body);
    let _body = req.body;
    //必要参数处理
    if (!(_body.name && _body.pwd && _body.password)) {
        let _result = res_format.response_without_request({
            cmd: "user/infos"
        });
        res.json(_result);

        return;
    }
    //参数 规则
    if (_body.pwd != _body.password) {
        let _result = res_format.response_error_request({
            cmd: "user/infos",
            msg: "密码输入不一致!"
        });
        res.json(_result);

        return;
    }

    //手机号
    if (/D+/.test(_body.phone)) {
        let _result = res_format.response_error_request({
            cmd: "user/infos",
            msg: "手机号错误!"
        });
        res.json(_result);

        return;
    }

    //sql
    user_service.sign(_body, function (data) {
        console.log("=============== router query callback ==========");
        console.log(data);
        // next(data);
        res.json(data);
    })
});
// 用户登录
router.post('/login', function (req, res) {
    console.log(req.body);
    let _body = req.body;
    //必要参数处理
    if (!(_body.name && _body.password)) {
        let _result = res_format.response_without_request({
            cmd: "user/infos"
        });
        res.json(_result);

        return;
    }
    //密码的 md5 处理

    //sql
    user_service.login(_body, function (data) {
        console.log("=============== router query callback ==========");
        console.log(data);
        res.json(data);
    })

});
// 查询用户信息
router.post('/infos', function (req, res) {
    console.log(req.body);
    let _body = req.body;
    if (!_body.name) {
        let _result = res_format.response_without_request({
            cmd: "user/infos",
            msg: "name is null",
        });

        res.json(_result);
        return;
    }

    //sql
    user_service.query(_body, function (data) {
        console.log("=============== router query callback ==========");
        console.log(data);

        //proxy 老后台查询
        var url = 'http://120.142.3.52:8088/uci-pre/unionpay/shop/getIndustryInfo.json';
        //req.pipe(request(url)).pipe(res);

        res.json(data);
    })

});

module.exports = router;

router.post('/infos', function (req, res) 最后一个信息查询中需要先查自己服务端的数据库,然后调用其他服务的接口(post)获取更多维度的信息

是需要第三方插件实现吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

必须使用 node-fetch 或者 request 插件?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...