javascript - 自定义静态路由不起作用

标签 javascript node.js reactjs express react-router

嗨,我正在尝试在 React 中建立我的项目。 我当前的项目结构是

-public
--w
---dist
----bundle.js
---index.html
-server
--server.js
-src
--app.js
-webpack.config.js
-package.json
-.babelrc

我使用 Node js 作为服务器 我希望在 localhost:port//w/ 上调用我的静态文件 以及 localhost:port//api/

上的 api 调用

我尝试过操作 server.js、路由、项目结构和 webpack.config 但未能成功。

服务器.js

const express = require('express');
const path = require('path');

const app = express();
const port = 3000;
const publicPath = path.join(__dirname, '../public/w');

app.use(express.static(publicPath));

app.get('/w/*', (req, res) => {
    console.log('Calling..');
    res.sendFile(path.join(publicPath, 'index.html'));
})

app.get('/api/test', (req, res) => {
    res.send("Hello");
})

app.listen(port, () => {
    console.log(`Server is up on ${port}`);
})

webpack.config

const path = require('path');

module.exports = {
    entry: './src/app.js',
    output: {
        path: path.join(__dirname, 'public', 'w', 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            { 
                test: /\.js$/, 
                use: 'babel-loader', 
                exclude: /node_modules/
            }
        ]
    },
    devtool: 'inline-source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public', 'w'),
        publicPath: '/dist/',
        historyApiFallback: true
    }
}

我的路线

const AppRouter = (props) => {
    return (
        <BrowserRouter>
            <div>
                <Switch>
                    <Route path="/" component={Dashboard} />
                    <Route path="/w/resume-builder" component={ResumeBuilder} />
                </Switch>
            </div>
        </BrowserRouter>
    )
}

任何人都可以建议我应该做什么或我缺少什么吗?

最佳答案

你必须进行一些重组

-public
--dist
---bundle.js
--index.html
-server
--server.js
-src
--app.js
-webpack.config.js
-package.json
-.babelrc

服务器.js

const express = require('express');
const path = require('path');

const app = express();
const port = 3000;
const publicPath = path.join(__dirname, '../public');

app.use(express.static(publicPath));

//keep all api before fallback
app.get('/api/test', (req, res) => {
    res.send("Hello");
});

app.get('/w/*', (req, res) => {
    console.log('Calling..');
    res.sendFile(path.join(publicPath, 'index.html'));
});

app.listen(port, () => {
    console.log(`Server is up on ${port}`);
});

webpack.config.js

const path = require('path');

module.exports = {
    entry: './src/app.js',
    output: {
        path: path.join(__dirname, 'public', 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            { 
                test: /\.js$/, 
                use: 'babel-loader', 
                exclude: /node_modules/
            }
        ]
    },
    devtool: 'inline-source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public'),
        publicPath: '/dist/',
        historyApiFallback: true
    }
}

您可以保持路线不变。

关于javascript - 自定义静态路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579725/

相关文章:

javascript - 如何使用 Jade 将 ui-view ="ui-view"制作为 html 中的 ui-view

javascript - onChange 触发时如何发出新的 axios 请求?

javascript - react 原生 redux 状态更新但不在组件中渲染

javascript - AngularJS 无法使用我的 html

javascript - 访问 DOM 节点的引用时,React Refs 返回 null 或未定义

javascript - 如何发送文本区域的内容?

javascript - ngFlow 到 Forimidable node.js 服务器

javascript - 启用和禁用触摸

reactjs - 尝试在我的 React 应用程序中使用 cookie,但它一直被重新渲染

java - React 和 Freemarker 集成