我的server.js代码:
var express = require('express');
var app = express();
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/dist'));
if (par.query.login) {
var data={
"user": {
"displayName": "fares alkhawaja",
"username": "elkhawajah"
},
"profile": {
"photo":"null",
"fullName": "fares sameer alkhawaja"
},
"balance": {
"overall": 200,
"outstanding": 149
},
"currentTasks": [{
"isProject": false,
"id": "1234",
"title": "Build NodeJS Application",
"description": "This is a description for building nodejs application",
"prize": "$500",
"project": "topcoder",
"holder": "elkhawajah"
}, {
"id": "134",
"isProject": false,
"title": "Build NodeJS Application",
"description": "This is a description for building nodejs application",
"prize": "$500",
"project": "topcoder",
"holder": "elkhawajah"
}, {
"id": "12",
"isProject": true,
"title": "Build NodeJS Application",
"description": "This is a description for building nodejs application",
"prize": "$500",
"project": "topcoder",
"holder": "elkhawajah"
}],
"pastTasks": [{
"isProject": false,
"id": "1",
"title": "Build NodeJS Application",
"description": "This is a description for building nodejs application",
"prize": "$500",
"project": "topcoder",
"holder": "fares"
}, {
"id": "2",
"isProject": false,
"title": "Build NodeJS Application",
"description": "This is a description for building nodejs application",
"prize": "$500",
"project": "topcoder",
"holder": "fares"
}, {
"id": "3",
"isProject": true,
"title": "Build NodeJS Application",
"description": "This is a description for building nodejs application",
"prize": "$500",
"project": "topcoder",
"holder": "fares"
}],
"timeline": [{
"title": "Fares uploaded a new submission",
"project": "tasqat",
"task": "Build new endpoint",
"date": new Date(),
"handle": "fares"
}, {
"title": "Fares uploaded a new submission",
"project": "tasqat",
"task": "Build new endpoint",
"date": new Date(),
"handle": "fares"
}]
}
}
app.get("/dashboard", function(req, res) {
//send Jsondata to /view/dashboard.ejs
res.render('dashboard', data);
});
app.listen(process.env.PORT);
console.log(process.env.PORT + ' is the magic port');
我的问题是我需要使用req.query.login ...如果req.query.login {return data} else {return nothing}
例如..当我使用http://localhost:3000/dashboard数据时,如果用户登录http://localhost:3000/dashboard?login=true应该返回数据,那么应该只返回任何帮助吗?
答案 0 :(得分:0)
根据您的要求,具有query params
var userData = {...}; //your data for user
您的控制器将是这样的
app.get("/dashboard", function(req, res) {
var isLoggedIn = req.query.login; //assuming route as /dashboard?login=true
var data = isLoggedIn? userData : {}; //empty object or null, whatever you want
res.render('dashboard', data);
});