【问题标题】:Validate input in Nodejs/Express back-end and send result to React front-end在 Nodejs/Express 后端验证输入并将结果发送到 React 前端
【发布时间】:2018-04-26 14:33:06
【问题描述】:

我想检查用户在我的 React 前端输入的域(例如google.com)是否有效。
我将域发送到我的 Nodejs/Express 后端并使用 Node 函数dns.lookup 来检查域是否有效,如下所示:

app.post('/new-cert', function (req, res) {
    var lookup = dns.lookup(req.body.deletedCert, function (err, addresses, family) {
        console.log(addresses); //eg.74.125.224.72 or undefined
    });

    // Only run this bit if `addresses` above is NOT `undefined`
    fs.appendFile('certs-list', '\n' + req.body.domainInput, function (err) {
        res.send('POST request: ');
        exec('sh cert-check-script-insert.sh');
        if (err) throw err;
    });
});

如果 addressesundefined,那么我想告诉我的 React 前端输入的域无效,然后它可以向用户打印相关消息。
否则我想从fs.appendFile 开始运行其余的函数以继续插入域。

对不起,我是 React/Node 的新手,找不到可以帮助我的帖子,感谢任何帮助!

【问题讨论】:

    标签: node.js reactjs validation express post


    【解决方案1】:

    例子:

    快递:

    if (addresses) {
        res.send(addresses)
    } else {
        res.status(404).send({message: "Address not found with " + req.body.deletedCert})
    }
    

    反应:

    const response = await fetch('/new-cert', config)
    if (response.ok) {
        const json = await response.json()
        this.setState({adresses: json})
    } else {
        alert('Could not find the domain.')
        return
    }
    

    【讨论】:

    • 谢谢!会试试这个
    • 这会在响应不正常的情况下阻止应用程序,如何处理错误但确保应用程序继续运行?
    • 这只是一个例子。删除return,它将继续。
    猜你喜欢
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2016-12-27
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多