gpt4 book ai didi

node.js - Nodejs Express : Sending a different page if logged in

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:44 25 4
gpt4 key购买 nike

我正在尝试使用expressjs 创建一个网站。如果用户已登录,我想向他发送不同的页面。

我可以发送原始 html 网站,但未加载像 index.css 这样的链接文件!

app.use(cookieParser());
app.use(expressSession({secret:'somesecrettokenhere', resave: false, saveUninitialized: true}));
app.use(bodyParser());

//app.use(express.static(path.join(__dirname, '../client/login')));
// disable layout
var mainPage = express.static(path.join(__dirname, '../client/main'));
var loginPage = express.static(path.join(__dirname, '../client/login'));

app.get('/', function (req, res, next) {
console.log('getting /');
if (req.session.userName) {
//app.use(express.static(path.join(__dirname, '../client/main')));
console.log("logged in");
mainPage(req, res, next);
}else {
//app.use(express.static(path.join(__dirname, '../client/login')));
console.log("not logged in");
loginPage(req, res, next);
}
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

提前致谢!

最佳答案

是的,可以。您可以执行 response.render(pageName); 来呈现您想要向用户发送的页面。

app.use(cookieParser());
app.use(expressSession({secret:'somesecrettokenhere', resave: false, saveUninitialized: true}));
app.use(bodyParser());

//app.use(express.static(path.join(__dirname, '../client/login')));
// disable layout
// view engine setup
app.set('views', path.join(__dirname, '../client'));
app.set('view engine', 'jade');
app.get('/', function (req, res, next) {
console.log('getting /');
if (req.session.userName) {
//app.use(express.static(path.join(__dirname, '../client/main')));
console.log("logged in");
res.render("mainPage");
}else {
//app.use(express.static(path.join(__dirname, '../client/login')));
console.log("not logged in");
res.render("loginPage");
}
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

我使用的是jade模板引擎,你可以使用任何其他的

关于node.js - Nodejs Express : Sending a different page if logged in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27531448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com