如何使用Express.js渲染HTML文件?

时间:2018-12-16 10:32:06

标签: node.js express verify

我是Node js开发的新手。 我正在使用bycript和验证邮件开发登录/注册过程。 我已经使用以下代码实现了通过邮件发送的验证令牌:

router.get('/verification/:token', function(req, res) {

// Check for validation errors
var errors = req.validationErrors();
if (errors) return res.status(400).send(errors);

// Find a matching token
Token.findOne({ token: req.params.token }, function (err, token) {
if (!token) return res.status(400).send({status: 'ko',error: {type: 'not-verified', msg: 'We were unable to find a valid token. Your token my have expired.'}} );

// If we found a token, find a matching user
User.findOne({ _id: token._userId }, function (err, user) {
  if (!user) return res.status(400).send({ msg: 'We were unable to find a user for this token.' });
  if (user.isVerified) return res.status(400).send({status: 'ko',error:{type: 'already-verified', msg: 'This user has already been verified.'}});

  // Verify and save the user
  user.isVerified = true;
  user.save(function (err) {
    if (err) { return res.status(500).send({ msg: err.message }); }
    res.status(200).send({status: 'ok', data: { msg: 'The account has been verified. Please log in.'}});
        });
    });
});

});

此代码可以正常工作,但向我显示了一条向浏览器的消息。 单击验证URL时,我想呈现一个特定的HTML页面,该页面显示消息OK。 我怎样才能做到这一点? 我创建了一个名为html / welcome.html

的文件夹

1 个答案:

答案 0 :(得分:1)

HTML文件是静态文件。您必须将Express设置为使用静态文件。

在您的情况下为HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://...")) .build(); HttpResponse<InputStream> response = client .sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()) .get(); // this finishes as soon as the header is received try { InputStream stream = response.body(); byte[] test = stream.readNBytes(20); // trying to read just a few bytes // but it waits for the whole body } catch (IOException ex) {} This是有关在Express中投放静态文件的文档