问题
How to create dynamic sitemap.xml file in Angular 9 project structure ? I can get data using HTTP get request from node API but using this data how can I update local xml file from angular project at runtime ?
In my case, angular project and node project are on different server(different domain). And It is possible to create xml file in node project but I am not able to access that file as xml file in angular project root .
Can anyone please suggest me how to use node xml in angular project OR is it possible to create/modify dynamic sitemap.xml using api call in Angular 9?
Any help would be greatly appreciated.
回答1:
I am working on the same subject now and I have achieved something.
I added it in the angular.json
file like this
Then I created a component and specified its path as sitemap.xml
.
It is possible to perform server-side rendering in Angular, and with that, our job is a little easier. Angular Server Side Rendering
You can add a new routing to your routing.module file and interfere with this request via server.ts
For example, you can access a route like sitemap.xml
in this way in server.ts
.
server.get('/sitemap.xml', (req, res, next) => { bla,bla }
You can now access the file you want here and change it as you wish.
File operations in server.ts
function writeFile(sitemap: string): void {
const fs = require('fs');
fs.appendFile('../sitemap.xml', sitemap, (err) => {
if (err) {
throw err;
}
console.log('Saved!');
});
}
then, redirect operations
res.redirect('http://localhost:{$PORT}/sitemap.xml');
来源:https://stackoverflow.com/questions/63966080/how-to-dynamically-create-modify-sitemap-xml-file-in-angular-9-project-structure