How to render html file with Express.js?

自作多情 提交于 2020-07-21 07:03:42

问题


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.'}});
        });
    });
});

});

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


回答1:


HTML files are static files. You have to set up Express to use static files.

in your case, that'd be app.use(express.static(__dirname+'/html')) This is documentation about serving static files in express



来源:https://stackoverflow.com/questions/53801317/how-to-render-html-file-with-express-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!