错误:未指定默认引擎,并且节点服务器中未提供扩展名

时间:2019-01-14 19:47:48

标签: node.js express passport.js

我的前端处于倾斜状态时,我正在使用nodejs作为后端服务器。我需要打电话给注册路由器,为此我要从json中的前端从atat发送数据,这是完美的,但是由于我要在此处进行护照认证,因此我从另一个github存储库中获取了护照认证代码,该节点中的节点也使用了引擎这就是为什么在其中使用res.render的原因。但是现在我发现很难从注册路由器中删除此特定行,并且如果我尝试删除它或仅使用res.sendStatus(500),它仍然不起作用,并且出现500错误。该问题的解决方案是什么?

var express = require('express');
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var cors = require('cors');
var session = require('cookie-session');
var flash = require('connect-flash');
var passport = require('passport');
var bcrypt = require('bcryptjs');

require('./config/passport')(passport);

var User = require('./models/User');
var app = express();

app.use(bodyparser.json());
app.use(cors());
var expiryDate = new Date(Date.now() + 60 * 60 * 1000) // 1 hour
app.use(session({
  name: 'session',
  keys: ['key1', 'key2'],
  cookie: {
    secure: true,
    httpOnly: true,
    domain: 'example.com',
    path: 'foo/bar',
    expires: expiryDate
  }
}))
app.set('port', process.env.port || 3000);
app.use(passport.initialize());
app.use(passport.session());


// Connect flash
app.use(flash());

// Global variables
app.use(function(req, res, next) {
  res.locals.success_msg = req.flash('success_msg');
  res.locals.error_msg = req.flash('error_msg');
  res.locals.error = req.flash('error');
  next();
});


var db = mongoose.connect("mongodb://localhost:27017/server", {
  useNewUrlParser: true
}, function(err, response) {
  if (err) {
    console.log('There is error in connecting with mongodb');
  }
  console.log('Connection has been established.');
});

app.get('/', (req, res) => {
  res.send("hello");
});


//Trying registering with passport

app.post('/register', (req, res) => {
    console.log(req.body);
    const { firstname, lastname, email, password } = req.body;
    let errors = [];

    if (errors.length > 0) {
      res.render('register', {
        errors,
        name,
        email,
        password,
        password2
      });
    } else {
      User.findOne({ email: email }).then(user => {
        if (user) {
          errors.push({ msg: 'Email already exists' });
          res.render('register', {
            errors,
            firstname,
            lastname,
            email,
            password
          });
        } else {
          const newUser = new User({
            firstname,
            lastname,
            email,
            password
          });

          bcrypt.genSalt(10, (err, salt) => {
            bcrypt.hash(newUser.password, salt, (err, hash) => {
              if (err) throw err;
              newUser.password = hash;
              newUser
                .save()
                .then(user => {
                  req.flash(
                    'success_msg',
                    'You are now registered and can log in'
                  );
                  res.redirect('/login');
                })
                .catch(err => console.log(err));
            });
          });
        }
      })
      .catch(err => console.log(err))
    }
  });

//end


app.post('/login', (req, res, next) => {
    console.log(req.body);
    passport.authenticate('local', {
      successRedirect: '/dashboard',
      failureRedirect: '/login',
      failureFlash: true
    })(req, res, next);
  });


app.listen(app.get('port'), function(err, response) {
    console.log("Server is running");
});

1 个答案:

答案 0 :(得分:1)

您正在app.post('/ register',callback)的回调函数中使用res.render()。如果调用res.render(),它将寻找一个模板(可能在views目录中)。如果您未指定要使用哪个引擎,则会收到该错误。