Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Angular 5 Universal获取域始终返回127.0.0.1_Node.js_Angular_Nginx - Fatal编程技术网

Node.js 从Angular 5 Universal获取域始终返回127.0.0.1

Node.js 从Angular 5 Universal获取域始终返回127.0.0.1,node.js,angular,nginx,Node.js,Angular,Nginx,我对Angular 5应用程序的服务器端渲染有问题。我需要域来创建适当的链接(应用程序在不同的环境中使用,为每个端点创建几个包是个坏主意) 所以我尝试使用堆栈中的选项,但我总是得到127.0.0.1:4000作为域/主机。我认为最好的选择应该是: server.ts app.engine('html', (_, options, callback) => { const engine = ngExpressEngine({ bootstrap: AppServerM

我对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.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中选中object
选项
,它没有任何域信息<代码>主机在那里定义为
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.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的另一个答案: 和部分Nginx文档: