案例
当我访问我的 url 时https://example.com/
,我的 angular 应用程序加载并且 url 将哈希路由添加到https://example.com/#/
. 我的 Angular 代码检查来自 saml url 的用户凭据,但没有找到任何东西,因此它调用护照登录,然后将我带到我的 SAML 登录页面(公司托管),该页面再次返回我的主页,并提供正确的用户详细信息。
我的问题是在执行我的app.get (*) catch all 以通过 index.html 页面发送之前,能否防止 Angular应用程序加载并req.user
检查我的所有应用程序路由。
客户端路由可以做到这一点吗?下面看一下现在配置的 server.js。任何帮助将不胜感激。
var env = require('dotenv').config();
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var path = require('path');
var passport = require('./server/sso/passport')(app);
// Enforce HTTPS on all requests
app.enable('trust proxy');
app.use(function (req, res, next) {
if (req.headers['x-forwarded-proto'] === 'https') {
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
}
});
// Set Up
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// All responses return CORS allowed
app.use(function (req, res, next) {
if (req.headers.origin) { res.header('Access-Control-Allow-Origin', req.headers.origin); }
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
('OPTIONS' == req.method) ? res.send(200) : next();
});
// API Routes
app.use(require('./server/routes/book-routes'));
app.use(require('./server/routes/car-routes'));
// SSO
app.use(require('./server/sso/routes')(passport));
// Serve the client website.
app.use(express.static(path.resolve(__dirname, 'dist')));
// Route all unhandled requests to index.html, so client's router can handle them.
app.get('*', (req, res) => {
var authenticated = req.user;
if (authenticated) {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
} else {
res.redirect('/samlLogin', 301);
}
});
// Start server on the specified port and binding host
app.listen(appEnv.port, appEnv.bind, function () {
// Print a message when the server starts listening.
console.log("Serving taas-tools app at " + appEnv.url);
});
// Export the app and listener.
module.exports = app;