【问题标题】:How to render html file with Express.js?如何使用 Express.js 呈现 html 文件?
【发布时间】:2018-12-16 10:32:05
【问题描述】:

我是节点 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

的文件夹

【问题讨论】:

  • 你使用的是 express.js 作为框架吗?
  • 是的,我正在使用快递
  • 您可以按照 SanSolo 告诉您的方式进行操作,但这还没有结束。您需要一些视图引擎来在 HTML 中呈现您的数据。
  • @mehta-rohan 您不需要视图引擎来提供静态数据。
  • @SanSolo 谢谢,伙计。他也必须尽快渲染动态数据。这是下一步。

标签: node.js express verify


【解决方案1】:

HTML 文件是静态文件。您必须设置 Express 才能使用静态文件。

在你的情况下,那是app.use(express.static(__dirname+'/html')) This 是关于在 express 中提供静态文件的文档

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多