在angular2通用版上设置站点地图的路线 [英] set route for sitemap on angular2 universal

查看:91
本文介绍了在angular2通用版上设置站点地图的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何设置路线,以便可以通过URL访问站点地图:

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

< domain_name>/sitemap.xml

< 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.

推荐答案

将其添加到 src 文件夹中的 server.ts 文件中:-

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

更新:

至于 Angular Universal 2.1 (大约)
server.ts 具有以下导入路径

Update:

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

import * as path from 'path';

所以分辨率有点高

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

对于当前的角度通用版本2.6

server.ts 具有以下导入路径

import {join} from 'path';

因此分辨率移到了

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

这篇关于在angular2通用版上设置站点地图的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆