Skip to content

Commit

Permalink
update for angular/ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed Apr 17, 2024
1 parent 3adb21a commit e212bfe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
6 changes: 5 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
"core-js",
"raf",
"rgbcolor"
]
],
"server": "src/main.server.ts",
"ssr": {
"entry": "server.ts"
}
},
"configurations": {
"production": {
Expand Down
27 changes: 20 additions & 7 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import express from 'express';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import bootstrap from './src/main.server';
import { environment } from 'src/environments/environment';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
const distFolder = join(process.cwd(), 'dist/primeng/browser');
const indexHtml = join(distFolder, 'index.html');
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const indexHtml = join(serverDistFolder, 'index.server.html');

const commonEngine = new CommonEngine();

server.set('view engine', 'html');
server.set('views', distFolder);
server.set('views', browserDistFolder);

// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get(
'*.*',
express.static(distFolder, {
express.static(browserDistFolder, {
maxAge: '1y'
})
);
Expand All @@ -35,7 +36,7 @@ export function app(): express.Express {
bootstrap,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
publicPath: browserDistFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }]
})
.then((html) => res.send(html))
Expand All @@ -44,3 +45,15 @@ export function app(): express.Express {

return server;
}

function run(): void {
const port = process.env['PORT'] || 4000;

// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}

run();
16 changes: 0 additions & 16 deletions vercel.json

This file was deleted.

0 comments on commit e212bfe

Please sign in to comment.