set route for sitemap on angular2 universal

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.

Answers:

Answer

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');
Answer

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

Answer

Update to Angular 6 (universal) :

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.