将数据从路由发送到Node.js中的html

时间:2018-09-17 19:39:03

标签: html node.js web ejs

我目前正在开发一个nodejs项目,我想在index.ejs中显示一些内容,这取决于用户是否登录。我真的有谷歌它,但无济于事,如果您能告诉我什么地方不对,我将不胜感激。现在,它向我显示错误:isLoggedIn未定义

这是html

const Location = require('../models/location');

module.exports = (app, passport) => {



app.get('/', isLoggedIn, (req, res) => {
    res.render('index', {
         user: req.user, isLoggedIn: req.isLogged
    });
});


app.get('/profile', isLoggedIn, (req, res) => {
    res.render('profile', {
        user: req.user
    });
});

app.get('/profile', isLoggedIn, (req, res) => {
    res.render('profile', {
      user: req.user, isLoggedIn: req.isLogged
    });
});


app.get('/getUsername',isLoggedIn,(req, res) => {
    res.json({username: req.user.email});
});

app.get('/getLocations', isLoggedIn, (req, res) => {
    Location.find({user:req.user.email},function(err, locations){
        res.json({locations:locations});
    });
});

app.post('/location', (req, res) => {
    let location = new Location()
    console.log(req.body)
    location.user = req.body.email
    console.log("LLegue al post")
    location.latitude = req.body.latitude
    location.longitude = req.body.longitude
    console.log(location.user)
    console.log(location.latitude)
    console.log(location.longitude)

    location.save((err, LocationStored)=>{
        if(err) res.status(500).send({message: `Error al salvar en la base de datos: ${err}`})
    })

    res.json({message: "Exitasion"});
});   
};

function isLoggedIn(req, res, next) {
   if (req.isAuthenticated()){
      return next();
   }
 return res.redirect('/');
}

我的index.ejs是:

<% if (isLoggedIn) { %>
//code
  <% } else{ %>  
<p>you're logged in</p>
 <% } %>

0 个答案:

没有答案