Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Serving static content based on Angular's BASE_HREF #567

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import {enableProdMode} from '@angular/core';

// Express Engine
import {ngExpressEngine} from '@nguniversal/express-engine';
// Import module map for lazy loading
import {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';

import * as express from 'express';
import {join} from 'path';
import * as fs from 'fs';

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();
Expand All @@ -16,7 +20,11 @@ enableProdMode();
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');
const DIST_FOLDER = join(process.cwd(), 'dist'); // change this to '.' if you gonna publish only the dist files

const indexContents = fs.readFileSync(join(DIST_FOLDER, 'browser', 'index.html'), 'utf8');

const BASE_HREF = /<base.*href=\"([a-z\/]+)\".*?>/gmi.exec(indexContents)[1] || '/';

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main.bundle');
Expand All @@ -36,7 +44,7 @@ app.set('views', join(DIST_FOLDER, 'browser'));
// app.get('/api/**', (req, res) => { });

// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
app.use(BASE_HREF, express.static(join(DIST_FOLDER, 'browser'), {
maxAge: '1y'
}));

Expand Down