set route for sitemap on angular2 universal

懵懂的女人 提交于 2019-12-08 08:16:58

问题


How can we set the route so that sitemap can be access through URL :

< domain_name >/sitemap.xml

My app routing module is kind of like this :-

export function getHomeModule() {
  return System.import('./+home/home.module' + (process.env.AOT ? '.ngfactory' : ''))
    .then(mod => mod[(process.env.AOT ? 'HomeModuleNgFactory' : 'HomeModule')]);
}

@NgModule({
  imports: [
    RouterModule.forRoot([
      { path: '', redirectTo: '/home', pathMatch: 'full' }
    ], {preloadingStrategy: PreloadAllModules})
  ],
})

I am using angular universal starter kit Angular Universal Starter Kit.


回答1:


Add this to the server.ts file in the src folder :-

app.route('/sitemap.xml')
  .get((req, res) => {
    res.sendFile(path.resolve(path.join(__dirname,'/sitemap.xml')));
  });

Update:

As for Angular universal 2.1(around)
server.ts has following for importing path

import * as path from 'path';

so resolution was somewhat,

const ROOT = path.join(path.resolve(__dirname, '..'));

For the current angular universal version 2.6

server.ts has following for importing path

import {join} from 'path';

so resolution is shifted to,

const DIST_FOLDER = join(process.cwd(), 'dist');



回答2:


Try Tor Helgevord's article about angular server side rendering and dynamic sitemap here : http://www.syntaxsuccess.com/viewarticle/server-side-rendering-in-angular-2.0




回答3:


Update to Angular 6 (universal) :

app.route('/sitemap.xml')
  .get((req, res) => {
    res.sendFile(join(DIST_FOLDER,'sitemap.xml'));
  });


来源:https://stackoverflow.com/questions/44050555/set-route-for-sitemap-on-angular2-universal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!