我正在处理此代码Angular 5.2.10
并且Angular CLI 1.7.4
:
@Injectable()
export class FmCookieService {
constructor(@Optional() @Inject(REQUEST) private _request: Request) { }
...
}
我仅在服务器平台上注入由REQUEST
提供的令牌并使用其值。@nguniversal/express-engine
我正在尝试升级到Angular 6
并且Angular 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(评论)的原因,这里是@angular/devkit commit diff的变化。
我发现了一些我不太喜欢的解决方法,我最终做了类似(on 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 的推荐方法是什么?