如何在运行时动态创建/修改 angular 9 项目结构中的 sitemap.xml 文件? [英] How to dynamically create/modify sitemap.xml file in angular 9 project structure at runtime?
问题描述
如何在 Angular 9 项目结构中创建动态 sitemap.xml 文件?我可以使用来自节点 API 的 HTTP 获取请求获取数据,但如何使用此数据在运行时从 angular 项目更新本地 xml 文件?
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 ?
就我而言,角度项目和节点项目位于不同的服务器(不同的域)上.并且可以在节点项目中创建 xml 文件,但我无法在 angular 项目根目录中以 xml 文件的形式访问该文件.
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 .
谁能建议我如何在 angular 项目中使用 node xml 或者是否可以在 Angular 9 中使用 api 调用创建/修改动态 sitemap.xml?
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?
任何帮助将不胜感激.
推荐答案
我现在正在研究同一个主题,并且取得了一些成果.
I am working on the same subject now and I have achieved something.
我像这样在 angular.json
文件中添加了它
I added it in the angular.json
file like this
然后我创建了一个组件并将其路径指定为 sitemap.xml
.
Then I created a component and specified its path as sitemap.xml
.
可以在 Angular 中执行服务器端渲染,有了它,我们的工作就轻松了一些.Angular 服务器端渲染
It is possible to perform server-side rendering in Angular, and with that, our job is a little easier. Angular Server Side Rendering
您可以向routing.module 文件中添加新路由,并通过server.ts
比如你可以在server.ts
中通过这种方式访问sitemap.xml
这样的路由.
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 }
您现在可以在此处访问所需的文件并根据需要进行更改.
server.ts
function writeFile(sitemap: string): void {
const fs = require('fs');
fs.appendFile('../sitemap.xml', sitemap, (err) => {
if (err) {
throw err;
}
console.log('Saved!');
});
}
然后,重定向操作
res.redirect('http://localhost:{$PORT}/sitemap.xml');
这篇关于如何在运行时动态创建/修改 angular 9 项目结构中的 sitemap.xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!