使用 Typescript 热重载 IIS Web 服务器做出反应

2024-02-15

在使用 C# Web Api 后端使用 TypeScript 开发 React 应用程序时,我想使用热重载。我使用 .Net 框架而不是 Core,所以我需要使用 IIS 或 IIS Express。我可以使用前端热重载webpack dev server没有问题,但我无法访问 API 资源。我能实现这个目标吗?


使用找到了解决方案webpack dev server as 反向代理 https://en.wikipedia.org/wiki/Reverse_proxy到 IIS。

NPM:

npm install --save react-hot-loader@next

npm install webpack-dev-server --save-dev

webpack.config.js,proxy是IIS运行的地方:

var webpack = require('webpack');
var path = require("path");
var proxy = 'localhost:61299';

module.exports = {
    entry: [
        // activate HMR for React
        'react-hot-loader/patch',

        // the entry point of our app
        './Scripts/src/index.tsx',
    ],
    //entry: "./Scripts/src/index.tsx",
    output: {
        filename: "./Scripts/dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
            //{ test: /\.tsx?$/, loader: "ts-loader" }
            { test: /\.tsx?$/, loader: ['react-hot-loader/webpack', 'ts-loader'] }
        ]
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        // enable HMR globally

        new webpack.NamedModulesPlugin(),
        // prints more readable module names in the browser console on HMR updates
    ],

    devServer: {
        proxy: {
            '*': {
                target: 'http://' + proxy,
            }
        },
        port: 8080,
        host: '0.0.0.0',
        hot: true,
    },
}

然后,我可以在项目根文件夹中使用以下命令启动 Web 服务器:node_modules\.bin\webpack-dev-server。如果我随后访问http://localhost:8080/我进行了热重载,并且仍然可以使用 C# Web api,因为它将在“”处代理 IIS Expresshttp://本地主机:61299/ http://localhost:61299/"

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Typescript 热重载 IIS Web 服务器做出反应 的相关文章

随机推荐