如何使用Express.js渲染HTML文件? [英] How to render html file with Express.js?
问题描述
我是Node js开发的新手. 我正在使用bycript和验证邮件开发登录/注册过程. 我已经使用以下代码实现了通过邮件发送的验证令牌:
I'm new on node js develop. I'm developing a Login / Signup process using bycript and verification mail. I have implemented the verification token send by mail using this code:
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
this code works fine, but show me a message into the browser. I would like to render a specific HTML page when clicking on verify URL, that shows the message OK. How can I do this? I have created a folder called html/welcome.html
推荐答案
HTML文件是静态文件.您必须将Express设置为使用静态文件.
HTML files are static files. You have to set up Express to use static files.
在您的情况下为app.use(express.static(__dirname+'/html'))
这是有关在Express中投放静态文件的文档
in your case, that'd be app.use(express.static(__dirname+'/html'))
This is documentation about serving static files in express
这篇关于如何使用Express.js渲染HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!