Angular CLI 6,Universal:从ExpressJS请求对象获取cookie的正确方法是什么?

时间:2018-06-04 20:46:18

标签: angular angular-cli angular6 angular-universal angular-cli-v6

我已将此代码用于Angular 5.2.10Angular CLI 1.7.4

@Injectable()
export class FmCookieService {
  constructor(@Optional() @Inject(REQUEST) private _request: Request) { }

  ...
}

我正在注入REQUEST提供的@nguniversal/express-engine令牌,并仅在服务器平台上使用其值。

我尝试升级到Angular 6Angular CLI 6,并且在编译时遇到以下错误:

ERROR in ./node_modules/cookie-signature/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Projects\POCs\cli6-test\node_modules\cookie-signature'
ERROR in ./node_modules/etag/index.js
Module not found: Error: Can't resolve 'crypto' in 'C:\Projects\POCs\cli6-test\node_modules\etag'
ERROR in ./node_modules/destroy/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Projects\POCs\cli6-test\node_modules\destroy'
ERROR in ./node_modules/etag/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Projects\POCs\cli6-test\node_modules\etag'
ERROR in ./node_modules/express/lib/view.js
Module not found: Error: Can't resolve 'fs' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/send/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Projects\POCs\cli6-test\node_modules\send'
ERROR in ./node_modules/send/node_modules/mime/mime.js
Module not found: Error: Can't resolve 'fs' in 'C:\Projects\POCs\cli6-test\node_modules\send\node_modules\mime'
ERROR in ./node_modules/express/lib/request.js
Module not found: Error: Can't resolve 'http' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/express/lib/response.js
Module not found: Error: Can't resolve 'http' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/express/lib/application.js
Module not found: Error: Can't resolve 'http' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/express/lib/request.js
Module not found: Error: Can't resolve 'net' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/content-disposition/index.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\content-disposition'
ERROR in ./node_modules/express/lib/response.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/express/lib/view.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/express/lib/application.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\express\lib'
ERROR in ./node_modules/mime-types/index.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\mime-types'
ERROR in ./node_modules/send/index.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\send'
ERROR in ./node_modules/send/node_modules/mime/mime.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\send\node_modules\mime'
ERROR in ./node_modules/serve-static/index.js
Module not found: Error: Can't resolve 'path' in 'C:\Projects\POCs\cli6-test\node_modules\serve-static'
ERROR in ./node_modules/destroy/index.js
Module not found: Error: Can't resolve 'stream' in 'C:\Projects\POCs\cli6-test\node_modules\destroy'
ERROR in ./node_modules/iconv-lite/lib/streams.js
Module not found: Error: Can't resolve 'stream' in 'C:\Projects\POCs\cli6-test\node_modules\iconv-lite\lib'
ERROR in ./node_modules/iconv-lite/lib/extend-node.js
Module not found: Error: Can't resolve 'stream' in 'C:\Projects\POCs\cli6-test\node_modules\iconv-lite\lib'
ERROR in ./node_modules/send/index.js
Module not found: Error: Can't resolve 'stream' in 'C:\Projects\POCs\cli6-test\node_modules\send'
ERROR in ./node_modules/body-parser/lib/read.js
Module not found: Error: Can't resolve 'zlib' in 'C:\Projects\POCs\cli6-test\node_modules\body-parser\lib'

这应该发生在这个新版本中,以下是#9827 (comment)@angular/devkit commit diff更改http raw data的原因。

我发现了一些我不太喜欢的解决方法,最后我做了一些事情(server.ts):

app.engine('html', (_, options, callback) => {
  renderModuleFactory(AppServerModuleNgFactory, {
    document: template,
    url: options.req.url,
    extraProviders: [
      provideModuleMap(LAZY_MODULE_MAP),
      { provide: COOKIES_TOKEN, useValue: options.req.cookies }
    ]
  }).then(html => {
    callback(null, html);
  });
});

然后,我在服务器平台上使用COOKIES_TOKEN,但我不确定推荐的方法是什么。

有人能指出我在服务器平台上从ExpressJS请求访问cookie的推荐方法是什么?

0 个答案:

没有答案