在快速请求参数中将1转换为undefined

时间:2019-04-27 19:18:35

标签: node.js express

我尝试在express中实现缩短的url,所以我有一个数组将url推入其中。
那么我有一个以数字作为短网址的get路由,但是我遇到了问题。
如果数字不是1,则可以正常工作,但如果数字为1,则未定义。 代码

total

通过短网址进入页面的代码

app.post("/api/shorturl/new",function(req,res){
    let urlindex=req.body.url.indexOf("http://");
    if (urlindex===-1){
        urlindex=req.body.url.indexOf("https://");
        if (urlindex===-1){
             res.json({"error":"invalid error"});
        }
        else{
            //https url
            url=req.body.url.substring(urlindex+8);
        }
    }
    else{
        //http url
        url=req.body.url.substring(urlindex+7);
    }


        dns.lookup(url,function(err,addresses){
            if (err){
                res.json({"error":"invalid error"});
                console.log(url);
            }
            else{
                urls.push(req.body.url);
                res.json({"oroginalUrl":`${req.body.url}`,"shortendUrl":`${urls.length}`});
            }
        });

});
  

当:shorturl为1
时   控制台日志
  {shorturl:“ undefiened”}
  如果是4或2,包括0
  {shorturl:“数字”}

0 个答案:

没有答案