从服务器向客户端发送操纵/创建的响应



iam使用
https://github.com/peteward44/node-svn-ultimate
而且它没有承诺发展。但是我想从nodejs服务器发送成功或错误的结果。
通常这样做

    // Client -> AngularJs Request
    svn()
    {
      this.$http.post(...)
        .then(res => {
          console.log(res);
        },err=>{
          console.log(err);
        });
    }  
**Server**  
//  index.js
'use strict';
var express = require('express');
var router = express.Router();
router.post('/svn', controller.svn);//the route to use
module.exports = router;    

//controller.js
function respondWithResult(res, statusCode) {//respond to client
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      res.status(statusCode).json(entity);
    }
  };
}
...
export function svn(req, res) {
      return DB.find({...})//Sequelize Query
    })//returning res as promise
    .then(res => {
        svnUltimate.commands.commit(..., (err, success) => {
                  if (err) {
                    console.log('commit error')
                    console.log(err)
                  } else {
                    console.log(success);
                })
        return res;
      })
    })
    .then(respondWithResult(res))
    .catch(handleError(res));
}

现在正在响应数据库查询的结果,但我希望它响应SVN结果。
尝试了几件事,例如

svnUltimate.commands.commit(..., (err, success) => {
                      if (err) {
                        res=err;
                      } else {
                        //res=success;// answer was still the DB query
                        //return success; // no output
                        //res.status(404).send('crazy');// is no function
                        //res.status = 404 //no output
                        // res.writeHeader()// no function
                      })
            return res;
          })

SVN命令结果的结果如何回应客户?
修改续集的res
以某种方式写自己的响应?
还是有带有承诺的SVN软件包?

仅使用Nodejs 6.9.5
Express 4.0
续集3.28

要返回数据我使用以下方法。

res.status = 200;
res.json({myData: "example");

如果您有错误,则需要发送类似的内容:

res.status = 400;
res.JSON({message: "error. Invalid request"})

您需要确定要从Web服务器返回的数据类型。上面的示例使用JSON,最适合JavaScript堆栈。

最新更新