微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node.js – 从Angular 5 Universal获取域总是返回127.0.0.1

我对Angular 5应用程序的服务器端渲染有问题.我需要域来创建适当的链接(应用程序在不同的环境中使用,并且为每个端点创建几个包是个坏主意).

所以我尝试使用堆栈中的选项,但总是得到127.0.0.1:4000作为域/主机.我认为最好的选择应该是:

server.ts

app.engine('html', (_, options, callback) => {
    const engine = ngExpressEngine({
        bootstrap: AppServerModuleNgFactory,
        providers: [
            { provide: 'request', useFactory: () => options.req, deps: [] },
            provideModuleMap(LAZY_MODULE_MAP)
        ]
    });
    engine(_, options, callback);
});

在app.component.ts中:

   constructor(@Inject(PLATFORM_ID) private platformId,
                private injector: Injector) {

        if (isPlatformServer(this.platformId)) {
            let req = this.injector.get('request');
            console.log("host: " + req.get('host'));
        } else {
            console.log('we\'re rendering from the browser, there is no request object.');
        }
    }

我也检查了NodeJS中的对象选项 – >它没有关于域的任何信息. host定义为127.0.0.1:4000.我不知道如何从NodeJS获取它.你能帮忙或给小提示吗?

也许我的错是我有错误Nginx配置?

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name mock.test.com;

    ssl_certificate /home/xxx/Projects/xsw/cert/certificate.chained.crt;
    ssl_certificate_key /home/xxx/Projects/xsw/cert/key.prv;

    root /home/xxx/Projects/xsw/;
    index index.html;

    location / {
        proxy_pass http://127.0.0.1:4202;
    }

}

解决方法:

我今天失去了大约10个小时,但最终我找到了解决方案.我的问题与Nginx配置有关.

我用的时候:

location / {
    proxy_pass http://127.0.0.1:4000;
}

Nginx将请求中的主机更改为127.0.0.1:4000.所以解决这个问题的方法是将Nginx代理编辑成:

location / {
    proxy_pass http://127.0.0.1:4000;
    proxy_set_header Host example.com;
    proxy_set_header HTTPS on;
}

我没有更改我的express node.js服务器中的任何内容

server.ts

app.engine('html', (_, options, callback) => {
    const engine = ngExpressEngine({
        bootstrap: AppServerModuleNgFactory,
        providers: [
            { provide: 'request', useFactory: () => options.req, deps: [] },
            provideModuleMap(LAZY_MODULE_MAP)
        ]
    });
    engine(_, options, callback);
});

在我的app.component.ts中,我使用此配置:

   private domain: string;

   constructor(@Optional() @Inject(REQUEST) private request: any,
               @Inject(PLATFORM_ID) private platformId,
               private injector: Injector) {

        if (isPlatformServer(this.platformId)) {
            if (this.request.get('https')) {
                this.domain = 'https://' + this.request.get('host');
            } else {
                this.domain = 'http://' + this.request.get('host');
            }
        } else {
            this.domain = document.location.protocol + '//' + document.location.hostname;
        }
    }

为了创建这个答案,我使用StackOverflow的另一个答案:https://stackoverflow.com/a/47434417/2972087
Nginx Docs的一部分:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐