简体   繁体   English

node.js&在异步数据库搜索后表达,重定向或发送文件

[英]node.js & express, redirect or send file after async db search

I am trying to write routes for my node.js & express application and having a little problem. 我正在尝试为我的node.js和express应用程序编写路由,但遇到了一些问题。

The logic is this: 逻辑是这样的:

  1. user enter website with special tag id /6758HDE 用户输入带有特殊标签ID /6758HDE网站
  2. i do async check in db if tag already exists 如果标签已经存在,我会在db中进行异步检查
  3. if tag exists i roll new unique tag and set url with it 如果标签存在,我会滚动新的唯一标签并使用它设置网址
  4. if tag doesnt exist i allow current tag and set url with it 如果标签不存在,我允许当前标签并使用它设置网址

so far this is my code: 到目前为止,这是我的代码:

app.get('/*', function(req, res) {
    check_in_db(req.url, function(result) {
        if(result) {
            res.sendFile(__dirname + '/index.html');
        } else {
            res.redirect('/' + roll_new_id());
        }
    });
});

I guess that code is not working because it does async db search while headers must be sent right away, i remember that there was something like next() to deal with this kind of situation but my knowledge is very weak, maybe someone could point me in right track and show how to change my code so it will work as intended. 我猜代码不起作用,因为它执行异步db搜索,而必须立即发送标头,我记得曾经有过类似next()事情来处理这种情况,但是我的知识很薄弱,也许有人可以指出我在正确的轨道上,展示如何更改我的代码,以使其按预期工作。

res.redirect happens before your roll_new_id function call has a chance to return the new id value. res.redirect发生在roll_new_id函数调用有机会返回新的id值之前。 To get around this you have to call roll_new_id first, and pass in a callback function that it will call once it is done creating the new id. 为了解决这个问题,您必须首先调用roll_new_id ,并传入一个回调函数,一旦创建完新ID,它将调用该函数。 Once the new id value has been created it will execute the callback function passing the new id value as an argument. 一旦创建了新的id值,它将执行回调函数,并将新的id值作为参数传递。 Inside the callback function that is passed to roll_new_id you'll do res.redirect using the new id value that was passed from roll_new_id when it executed the callback function. 内部传递给回调函数roll_new_id你会做res.redirect应用价值的新的ID从传递roll_new_id当它执行的回调函数。 Does this make sense? 这有意义吗?

function roll_new_id(callbackFunction){

  var new_id;

  // create the new id...

  // pass new_id to the callback function..
  callbackFunction(new_id); 

}

app.get('/*', function(req, res) {

    check_in_db(req.url, function(result) {

        if(result) {

            res.sendFile(__dirname + '/index.html');

        } else {


            roll_new_id(function(theNewId){

              // this is the callbackFunction
              // called from inside the roll_new_id function

              res.redirect('/' + theNewId);

            })

        }
    });
});

You could also write it like this: 您也可以这样写:

function rollNewIDCallbackFunction(theNewId) {

  // this is the callbackFunction
  // called from inside the roll_new_id function

  res.redirect('/' + theNewId);

}

roll_new_id(rollNewIDCallbackFunction);

If you want to use the next function do this: 如果要使用next功能,请执行以下操作:

app.get('/*', function(req, res, next) {

    check_in_db(req.url, function(result) {

        if(result) {

            res.sendFile(__dirname + '/index.html');

        } else {

            next(); // call middleware to roll new id

        }
    });

}, function(req, res){ // middleware to roll the new id

  function rollNewIDCallbackFunction(theNewId) {

    // this is the callbackFunction
    // called from inside the roll_new_id function

    res.redirect('/' + theNewId);

  }

  roll_new_id(rollNewIDCallbackFunction);

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM