您好,我在解决NodeJS中的每个视图时遇到问题。目前我使用Handlebars作为服务器端模板。例如,要呈现视图,res.render(loginDir, {login: false, admin: false, header: "Welcome!!"});
我访问的每条路线都是 http://blablabla 。我希望它是 https://blablabla 。请帮助我...提前谢谢
以下是我的代码:
Uiroutes.ts
import express = require('express');
import path = require('path');
var app = express();
class Uiroutes {
get uiroutes() {
var rootManageDir = path.join(__dirname,'../../../client/management/manage');
var rootExecutionDir = path.join(__dirname,'../../../client/execution/execution');
var rootUserHomeDir = path.join(__dirname,'../../../client/userhome/userhome');
var loginDir = path.join(__dirname,'../../../client/login/login');
app.get('/management',this.adminAuth, (req: express.Request, res: express.Response) => {
res.render(rootManageDir, {login: true, admin: true, header: "Admin, Welcome back!"});
});
app.get('/execution',this.adminAuth, (req: express.Request, res: express.Response) => {
res.render(rootExecutionDir, {login: true, admin: true, header: "Admin, Welcome back!"});
});
app.get('/userhome', this.userAuth, (req: express.Request, res: express.Response) => {
let header = req.body.username + ', Welcome back!';
res.render(rootUserHomeDir, {login: true, admin: false, header: header});
});
app.get('/login', (req: express.Request, res: express.Response) => {
res.render(loginDir, {login: false, admin: false, header: "Welcome!!"});
});
app.get('/logout', this.logout, (req: express.Request, res: express.Response) => {
res.redirect('/login');
});
return app;
}
userAuth(req, res, next) {
if(req.session["user"]&&(req.session["user"].role == 'user')) {
req.body.username = req.session["user"].username;
next();
} else {
if(req.session["user"]&&(req.session["user"].role == 'admin')) {
req.body.username = req.session["user"].username;
res.redirect('/management');
} else {
res.redirect('/login');
}
}
}
adminAuth(req, res, next) {
//console.log(req.session);
if(req.session["user"]&&(req.session["user"].role == 'admin')) {
req.body.username = req.session["user"].username;
next();
} else {
if(req.session["user"]&&(req.session["user"].role == 'user')) {
req.body.username = req.session["user"].username;
res.redirect('/userhome');
} else {
res.redirect('/login');
}
}
}
logout(req, res, next) {
if(req.session["user"]){
console.log("logged in");
delete req.session["user"];
console.log(req.session["user"]);
next();
} else {
delete req.session;
next();
}
}
}
export = Uiroutes;
以上vars
仅用于引用.hbs视图目录的文件。
var rootManageDir = path.join(__dirname,'../../../client/management/manage');
var rootExecutionDir = path.join(__dirname,'../../../client/execution/execution');
var rootUserHomeDir = path.join(__dirname,'../../../client/userhome/userhome');
var loginDir = path.join(__dirname,'../../../client/login/login');
在我的server.ts
//Set view engine
app.set('view engine', 'hbs');
var defaultDir = path.join(__dirname, '../client/layout/default');
var layoutDir = path.join(__dirname, '../client/layout');
var viewpath = path.join(__dirname, '../client')
//Config the view engine
app.engine('hbs', hbs.express4({
defaultLayout: defaultDir,
layoutsDir: layoutDir
}));
//config view path
app.set('views', viewpath);
app.use(new UIRoutes().uiroutes);
以下是服务器端的cert配置,以便使用https
在server.ts
const fs = require('fs');
var app = require('../server').app;
const https = require('https');
var ip = '0.0.0.0';
var port = 8443;
var privateKey = fs.readFileSync('/opt/epaas/certs/key');
var certificate = fs.readFileSync('/opt/epaas/certs/cert');
var ca = fs.readFileSync('/opt/epaas/certs/ca');
var pass = fs.readFileSync('/opt/epaas/certs/pass','ascii');
var options = {
key: privateKey,
cert: certificate,
ca: ca,
passphrase: pass,
requestCert: true,
rejectUnauthorized: false
};
var server = https.createServer(options, app);
server.listen(port, function(){
console.log('This app is listening on port:' + port);
});
我们的服务器是云,所有应用程序都在那里部署。很难说这是什么类型的服务器......
答案 0 :(得分:0)
您可以尝试使用HTTP到HTTPS的所有路由的重定向。
在所有路线前添加一行:
app.get('*', (req, res, next) => {
if(req.headers['x-forwarded-proto'] !== 'https') {
res.redirect('https://' + req.headers.host + req.url);
}
next();
});
如果请求是http,则检查是否显示标题;如果是http,则检查是否将用户重定向到https。
答案 1 :(得分:0)
您可以使用express-enforces-ssl来处理此问题:
const fs = require('fs');
const https = require('https');
const http = require('http');
const express = require('express');
const express_enforces_ssl = require('express-enforces-ssl');
const app = express();
/*
* Trust proxy must be set if you're behind a reverse proxy / load balancer
*
*/
app.enable('trust proxy');
app.use(express_enforces_ssl());
/*
* Then you have to make sure your express server can handle https
*/
http.createServer(app).listen(80);
https.createServer({
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
}, app).listen(443);