簡體   English   中英

在開發模式下代理服務器請求時,是否可以渲染不屬於 react 項目的 html 文件?

[英]Is it possible to render a html file that doesn't belong to the react project while proxying servers request on development mode?

我正在做一個 React 項目,但我的老板想讓我在項目中添加一個第三方 index.html 文件。 這個想法是創建一個重定向到另一個頁面的按鈕。 我在服務器中創建了一個端點

app.get('/openscad', function(req, res) {
  res.sendfile(path.join(__dirname, '/openscad/index.html'));
});

每次請求 /openscad 時返回 index.html 文件。 我啟動了服務器並做出反應(在開發中)

npm start
node server.js

我將請求從 localhost:3000 代理到服務器 (localhost:5000)。 當我直接向 localhost:5000 請求 /openscad 時,它會呈現我的 html,但是當我向 localhost:3000 請求它時,它不會呈現。 我只是得到一個 http 狀態 200 OK,但沒有返回 html。

這是我的反應索引文件

import React, { Component } from "react";
import ReactDOM from 'react-dom';
import Viewer from './viewer';
import Login from './login';
import {
  BrowserRouter as Router,
  Switch,
  Route
} from "react-router-dom";
import HomePage from "./homePage";
import ProfilePage from "./profilePage";

class App extends Component {

  render() {
    return (
      <Router>
        <Switch>
          <Route path="/viewer">
            <Viewer />
          </Route>
          <Route path="/access">
            <Login />
          </Route>
          <Route path="/profile">
            <ProfilePage />
          </Route>
          <Route path="/home">
            <HomePage />
          </Route>
        </Switch>
      </ Router>    
    )
  }
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

我希望它在開發中呈現,這樣我就不必每次想要測試這個新功能時都構建我的項目。 請求 localhost:3000 時是否可以渲染我的 index.html?

據我所知,create react app server 只為每個不是靜態文件的請求返回 index.html。 嘗試將您的第三方 html 放在 /public 中,然后重定向到完整路徑( /public/other.html )。

如果這不起作用,您將不得不代理或彈出以更改配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM