我们有一个Angular 2应用程序向.net Web API休息服务发出http请求。角度2应用程序和API都托管在IIS中。
客户端使用webpack进行热模块更换,捆绑等,并使用webpack的服务器端预留NodeJS
我遇到了作为服务器端预渲染一部分的http请求的问题。 API要求对所有请求进行Windows身份验证,并且服务器端请求失败,因为请求未经过身份验证。似乎节点没有提供相关的标头/用户令牌。
有关信息,这是我的webpack配置:
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
stats: { modules: false },
context: __dirname,
resolve: { extensions: [ '.js', '.ts' ] },
output: {
filename: '[name].js',
publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, include: /ClientApp/, use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
module: {
loaders: [
{
test: /config.js$/,
loader: "file?name=[config.js]&context=./dist/"
}
]
},
entry: { 'main-client': './ClientApp/boot-client.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin()
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot-server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
],
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
return [clientBundleConfig, serverBundleConfig];
};
这是发出http请求的打字稿代码(在路由保护中完成):
public resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<CallDetails> {
let id = route.paramMap.get('id');
let url: string = this.settings.apiUrl + `/api/call/get?id=${id}`;
let options = { withCredentials: true };
return this.http.get(url, options)
.takeUntil(this.ngUnsubscribe)
.map((res: Response) => {
return res.json() || {};
})
.map(data => {
return new CallDetails(data);
})
.catch((res: Response) => {
return this.loadError(res, id);
})
.toPromise<CallDetails>()
.then(callDetails => {
return callDetails
})
}
如何配置webpack / nodejs以对http请求使用Windows身份验证?另外,如何确保在当前用户的上下文中发出请求?
在浏览器中运行时一切正常。