我正在尝试从服务器端获取cookie,以处理@ ngx-utils / cookie的这些用途。 在浏览器中,它可以按预期工作,但在服务器中,cookies为空(已在isPlatformServer中验证)。
我做错了还是@ ngx-utils / cookies有问题?
我浏览了教程,指南和示例,但我无法完成这项工作,因此,如果您可以稍微指导一下我并且非常感谢,它将非常有用。
server.ts
import 'zone.js/dist/zone-node';
import { enableProdMode } from '@angular/core';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
import * as cookieParser from 'cookie-parser';
const compression = require('compression');
import * as express from 'express';
import { join } from 'path';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
enableProdMode();
const app = express();
app.use(compression());
app.use(cookieParser());
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist/browser');
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');
app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP)
]
}));
app.set('view engine', 'html');
app.set('views', DIST_FOLDER);
app.get('*.*', express.static(DIST_FOLDER, {
maxAge: '1y'
}));
app.get('*', (req, res) => {
res.render('index', {
req,
res,
providers: [
{
provide: REQUEST,
useValue: req
},
{
provide: RESPONSE,
useValue: res
}
]
});
});
app.server.module.ts
...
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
@NgModule({
imports: [
BrowserModule.withServerTransition({appId: 'frontend'}),
ServerCookiesModule.forRoot(),
AppModule,
ServerModule,
NoopAnimationsModule,
ModuleMapLoaderModule,
ServerTransferStateModule,
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TranslateInterceptor,
multi: true
},
{
provide: 'REQUEST',
useValue: REQUEST
},
{
provide: 'RESPONSE',
useValue: RESPONSE
}
],
bootstrap: [AppComponent],
})
export class AppServerModule {}