fix for 401 requests

This commit is contained in:
lotte
2018-11-27 11:45:35 +01:00
parent d46ab9637c
commit afa3a03d2c
3 changed files with 29 additions and 9 deletions

View File

@@ -17,6 +17,7 @@ import { ngExpressEngine } from '@nguniversal/express-engine';
import { ROUTES } from './routes';
import { ENV_CONFIG } from './config';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
const app = express();
@@ -31,9 +32,21 @@ export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
app.use(cookieParser());
app.use(bodyParser.json());
app.engine('html', ngExpressEngine({
bootstrap: bootstrap
}));
app.engine('html', (_, options, callback) =>
ngExpressEngine({
bootstrap: bootstrap,
providers: [
{
provide: REQUEST,
useValue: options.req,
},
{
provide: RESPONSE,
useValue: options.req.res,
},
],
})(_, options, callback)
);
app.set('view engine', 'html');
app.set('views', 'src');
@@ -53,8 +66,10 @@ export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
function ngApp(req, res) {
function onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
console.warn('Error in SSR, serving for direct CSR');
res.sendFile('index.csr.html', { root: './src' });
if (!res._headerSent) {
console.warn('Error in SSR, serving for direct CSR');
res.sendFile('index.csr.html', { root: './src' });
}
}
if (ENV_CONFIG.universal.preboot) {