-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammad Ahsan Ayaz
committed
Dec 8, 2024
1 parent
483af46
commit a9f1eac
Showing
12 changed files
with
117 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,4 +128,4 @@ | |
"@angular/common": "$@angular/common" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { mergeApplicationConfig, ApplicationConfig } from '@angular/core'; | ||
import { provideServerRendering } from '@angular/platform-server'; | ||
import { appConfig } from './app.config'; | ||
|
||
const serverConfig: ApplicationConfig = { | ||
providers: [ | ||
provideServerRendering() | ||
] | ||
}; | ||
|
||
export const config = mergeApplicationConfig(appConfig, serverConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { | ||
ApplicationConfig, | ||
} from '@angular/core'; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
|
||
], | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { AppComponent } from './app/app.component'; | ||
import { config } from './app/app.config.server'; | ||
|
||
const bootstrap = () => bootstrapApplication(AppComponent, config); | ||
|
||
export default bootstrap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
|
||
import { AppModule } from './app/app.module'; | ||
|
||
import { APP_ID, importProvidersFrom } from '@angular/core'; | ||
import { provideNoopAnimations } from '@angular/platform-browser/animations'; | ||
import { CommonModule } from '@angular/common'; | ||
import { AppComponent } from './app/app.component'; | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
platformBrowserDynamic() | ||
.bootstrapModule(AppModule) | ||
bootstrapApplication(AppComponent, { | ||
providers: [ | ||
importProvidersFrom(CommonModule), | ||
{ | ||
provide: APP_ID, | ||
useValue: 'serverApp' | ||
}, | ||
provideNoopAnimations(), | ||
] | ||
}) | ||
.catch(err => console.error(err)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { APP_BASE_HREF } from '@angular/common'; | ||
import { CommonEngine, isMainModule } from '@angular/ssr/node'; | ||
import express from 'express'; | ||
import { dirname, join, resolve } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import bootstrap from './main.server'; | ||
|
||
const serverDistFolder = dirname(fileURLToPath(import.meta.url)); | ||
const browserDistFolder = resolve(serverDistFolder, '../browser'); | ||
const indexHtml = join(serverDistFolder, 'index.server.html'); | ||
|
||
const app = express(); | ||
const commonEngine = new CommonEngine(); | ||
|
||
/** | ||
* Example Express Rest API endpoints can be defined here. | ||
* Uncomment and define endpoints as necessary. | ||
* | ||
* Example: | ||
* ```ts | ||
* app.get('/api/**', (req, res) => { | ||
* // Handle API request | ||
* }); | ||
* ``` | ||
*/ | ||
|
||
/** | ||
* Serve static files from /browser | ||
*/ | ||
app.get( | ||
'**', | ||
express.static(browserDistFolder, { | ||
maxAge: '1y', | ||
index: 'index.html' | ||
}), | ||
); | ||
|
||
/** | ||
* Handle all other requests by rendering the Angular application. | ||
*/ | ||
app.get('**', (req, res, next) => { | ||
const { protocol, originalUrl, baseUrl, headers } = req; | ||
|
||
commonEngine | ||
.render({ | ||
bootstrap, | ||
documentFilePath: indexHtml, | ||
url: `${protocol}://${headers.host}${originalUrl}`, | ||
publicPath: browserDistFolder, | ||
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }], | ||
}) | ||
.then((html) => res.send(html)) | ||
.catch((err) => next(err)); | ||
}); | ||
|
||
/** | ||
* Start the server if this module is the main entry point. | ||
* The server listens on the port defined by the `PORT` environment variable, or defaults to 4000. | ||
*/ | ||
if (isMainModule(import.meta.url)) { | ||
const port = process.env['PORT'] || 4000; | ||
app.listen(port, () => { | ||
console.log(`Node Express server listening on http://localhost:${port}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters