首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2 Universal + Akamai

Angular 2 Universal + Akamai
EN

Stack Overflow用户
提问于 2017-06-07 02:04:22
回答 1查看 508关注 0票数 0

我想看看我是否可以创建一个基于CDN和angular 2 universal的堆栈。因此,当用户导航具有获取资产的CDN时,如果用户第一次访问,则Universal将呈现完整的html。

我在想:

Client <===> Akamai <===> Varnish <===>源站(带通用的node.js)

这听起来不错吧?你试过吗?另外,我正在考虑为完整的堆栈添加nginx和ELB。

问题是:-这个堆栈能像预期的那样工作吗?

EN

回答 1

Stack Overflow用户

发布于 2017-07-07 06:28:16

是的,这是可以做到的!最大的问题是如何使Angular中任意数量的http请求无效,这些请求决定了呈现的页面。使用某种类型的头模式来使其无效可能会有所帮助。

假设您使用的是官方的ng-express引擎,这样的服务可以让您定义来自Angular运行时的响应:

代码语言:javascript
复制
import { RESPONSE } from '@nguniversal/express-engine/tokens'
import { Inject, Injectable, Optional } from '@angular/core'
import { Response } from 'express'

export interface IServerResponseService {
  getHeader(key: string): string
  setHeader(key: string, value: string): this
  setHeaders(dictionary: { [key: string]: string }): this
  appendHeader(key: string, value: string, delimiter?: string): this
  setStatus(code: number, message?: string): this
  setNotFound(message?: string): this
  setError(message?: string): this
}

@Injectable()
export class ServerResponseService implements IServerResponseService {

  private response: Response

  constructor(@Optional() @Inject(RESPONSE) res: any) {
    this.response = res
  }

  getHeader(key: string): string {
    return this.response.getHeader(key)
  }

  setHeader(key: string, value: string): this {
    if (this.response)
      this.response.header(key, value)
    return this
  }

  appendHeader(key: string, value: string, delimiter = ','): this {
    if (this.response) {
      const current = this.getHeader(key)
      if (!current) return this.setHeader(key, value)

      const newValue = [...current.split(delimiter), value]
        .filter((el, i, a) => i === a.indexOf(el))
        .join(delimiter)

      this.response.header(key, newValue)
    }
    return this
  }

  setHeaders(dictionary: { [key: string]: string }): this {
    if (this.response)
      Object.keys(dictionary).forEach(key => this.setHeader(key, dictionary[key]))
    return this
  }

  setStatus(code: number, message?: string): this {
    if (this.response) {
      this.response.statusCode = code
      if (message)
        this.response.statusMessage = message
    }
    return this
  }

  setNotFound(message = 'not found'): this {
    if (this.response) {
      this.response.statusCode = 404
      this.response.statusMessage = message
    }
    return this
  }

  setError(message = 'internal server error'): this {
    if (this.response) {
      this.response.statusCode = 500
      this.response.statusMessage = message
    }
    return this
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44396872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档