2016-08-15 77 views
2

我已經使用Webpack創建了我的第一個React應用程序。現在我想讓這個應用程序可以在我的開發環境之外訪問,但是我不能讓它工作。設置部署路線

對於我的發展我的本地計算機上運行:

"start": "node server.js",

這使我通過localhost:3000作出反應的應用程序訪問。這很好。

但現在我想部署我的反應的應用程序在一個域的外部服務器上。 要創建我的代碼包我打

"build": "webpack -p"這將生成子文件夾MyProjectFolder/dist/bundle.js中的包文件。之後,我將我的index.html複製到/dist/,幷包含該包文件。

<!DOCTYPE html> 
<html> 
<head> 
    <title>Urlaub-planer</title> 
<!-- Latest compiled and minified CSS --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 

<!-- Auth0Lock script --> 
<script src="//cdn.auth0.com/js/lock-9.0.min.js"></script> 
</head> 
<body> 
    <div class="todoapp" id="root"></div> 
    <!--<script src="/static/bundle.js"></script>--> 
    <script src="bundle.js"></script> 
    </body> 
    </html> 

這些都提交然後複製到我的外部服務器運行Apache。 服務器上的webfolder與我的ProjectFolder名稱相同。 我又試圖通過

`https://myDomain.de/MyProjectFolder/index.html

訪問我的應用程序這表明我這個錯誤:

Error MSG

我想,我必須改變我的路由器的設置,但我目前還沒有線索如何做到這一點。在我的開發環境中,項目文件夾不包含在URL中,當我訪問我的應用程序時,這可能是問題的一部分嗎?任何人都可以通過這個指導我嗎? 這個問題對於你們中的一些人來說似乎是非常基本的,但這是我第一次使用所有這些新技術,並且我已經嘗試了這麼多,而且仍然是同樣的錯誤信息。我需要做些什麼,在外部服務器上部署我的React App,運行Apache,使用不同的域。請幫幫我。

這是我的路由定義文件(ProjectFolder/index.js)

import React from 'react' 
import { render } from 'react-dom' 
import { createStore, applyMiddleware } from 'redux' 
import { Provider } from 'react-redux' 
import {Router, Route, IndexRoute, browserHistory} from 'react-router' 
import createLogger from 'redux-logger' 

import App from './containers/App' 
import VacationSummary from './containers/vacation/VacationSummary' 
import VacationRequest from './containers/vacation/VacationRequest' 
import VacationRequestSummary from './containers/vacation/VacationRequestSummary' 

import Home from './containers/Home' 
import Demo from './components/Demo' 
import rootReducer from './reducers/reducers' 
import thunkMiddleware from 'redux-thunk' 

var injectTapEventPlugin = require("react-tap-event-plugin"); 
injectTapEventPlugin(); 

const logger = createLogger(); 

    let createStoreWithMiddleware = applyMiddleware(thunkMiddleware, logger) (createStore) 

    let store = createStoreWithMiddleware(rootReducer) 

    let rootElement = document.getElementById('root') 

    if (module.hot) { 
    // Enable Webpack hot module replacement for reducers 
    module.hot.accept('./reducers',() => { 
    const nextRootReducer = require('./reducers').default 
    store.replaceReducer(nextRootReducer) 
    }) 
    } 

    render(
    <Provider store={store}> 
     <Router history={browserHistory}> 
     <Route path="/" component={App}> 
     <Route path="Home" component={Home}/> 
     <Route path="VacationSummary" component={VacationSummary}/> 
     <Route path="VacationRequest" component={VacationRequest}/> 
     <Route path="VacationRequestSummary" component= {VacationRequestSummary}/> 
    </Route> 
    </Router> 
    </Provider>, 
    rootElement 
) 

最後我的package.json

{ 
    "name": "Urlaubsplaner", 
    "version": "0.0.1", 
    "main": "index.js", 
    "scripts": { 
    "server": "node server/server.js", 
     "start": "node server.js", 
     "watch": "webpack --watch", 
     "production": "webpack -p" 
    }, 
    "author": "Auth0", 
    "license": "MIT", 
    "dependencies": { 
    "classnames": "^2.2.5", 
    "css-loader": "^0.23.1", 
     "material-ui": "^0.15.2", 
    "moment": "^2.13.0", 
    "react": "^15.1.0", 
    "react-bootstrap-daterangepicker": "^3.1.0", 
    "react-dom": "*", 
    "react-redux": "*", 
    "react-router": "*", 
     "react-tabs": "^0.7.0", 
     "react-tap-event-plugin": "^1.0.0", 
     "react-yearly-calendar": "^1.1.4", 
    "redux": "*", 
    "redux-form": "^5.2.5", 
    "redux-logger": "^2.6.1", 
     "redux-thunk": "*", 
    "style-loader": "^0.13.1" 
    }, 
    "devDependencies": { 
    "babel-core": "^5.6.18", 
    "babel-loader": "^5.1.4", 
    "babel-plugin-react-transform": "^1.1.0", 
    "express": "^4.13.3", 
    "webpack": "^1.9.11", 
    "webpack-dev-middleware": "^1.2.0", 
    "webpack-hot-middleware": "^2.2.0" 
    } 
    } 

而且我webpack.config

var path = require('path') 
var webpack = require('webpack') 

module.exports = { 
    devtool: 'cheap-module-eval-source-map', 
    entry: [ 
    'webpack-hot-middleware/client', 
    './index' 
    ], 
    output: { 
    path: path.join(__dirname, 'dist'), 
    filename: 'bundle.js', 
    publicPath: '/static/' 
    }, 
    plugins: [ 
    new webpack.optimize.OccurenceOrderPlugin(), 
    new webpack.HotModuleReplacementPlugin(), 
    new webpack.NoErrorsPlugin() 
    ], 
    module: { 
    loaders: [ 
     { test: /\.css$/, loader: "style-loader!css-loader" }, 
     {test: /\.js$/, 
     loaders: [ 'babel' ], 
     exclude: /node_modules/, 
     include: __dirname 
     }] 
    } 
    } 

更新:

我已經尋找解決方案,發現這github上線:Github 基於此信息,我已刪除了

history={browserHistory}從我的路由器,現在我看到至少我的應用程序,但我得到一個新的錯誤,那就是類似舊的。

New Error

讀消息後,我已經更新了我的代碼

import {Router, Route, IndexRoute, hashHistory} from 'react-router' 
<Provider store={store}> 
    <Router history={hashHistory}> 
    <Route path="/" component={App}> 
     <Route path="/Home" component={Home}/> 
     <Route path="/VacationSummary" component={VacationSummary}/> 
     <Route path="/VacationRequest" component={VacationRequest}/> 
     <Route path="/VacationRequestSummary" component={VacationRequestSummary}/> 
    </Route> 
    </Router> 

但I'm仍然收到上述錯誤信息。一個想法?

回答

0

...我已經尋找解決方案,發現這github上線:[Github上] [2] 基於此信息,我已經從我的路由器刪除

history={browserHistory}現在我看到至少我的應用程序,但我得到一個新的錯誤,這與舊的錯誤類似。

讀消息後,我已經更新了我的代碼

import {Router, Route, IndexRoute, hashHistory} from 'react-router' 
<Provider store={store}> 
    <Router history={hashHistory}> 
    <Route path="/" component={App}> 
     <Route path="/Home" component={Home}/> 
     <Route path="/VacationSummary" component={VacationSummary}/> 
     <Route path="/VacationRequest" component={VacationRequest}/> 
     <Route path="/VacationRequestSummary" component={VacationRequestSummary}/> 
    </Route> 
    </Router> 

但仍高於(更新Q)中提到的錯誤。我排除後

if (module.hot) { 
    // Enable Webpack hot module replacement for reducers 
    module.hot.accept('./reducers',() => { 
    const nextRootReducer = require('./reducers').default 
    store.replaceReducer(nextRootReducer) 
}) 
} 

錯誤消失了。現在,我仍然得到這個錯誤

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

但我認爲這無關與在提到的問題

1

如果您需要爲環境不同的配置,你可以創建不同的WebPack的配置文件,揭發全局常量爲DefinePlugin

如果你已經設置你的環境在您的系統或容器,你會寫這樣的:

new webpack.DefinePlugin({ 
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) 
}) 

這樣你就可以在你的陣營代碼訪問process.env.NODE_ENV,並在您方便的創建不同的路線。

+0

啊很好的建議。謝謝 – BayLife