2017-07-26 35 views
0

問題 - 我不能得到任何響應郵遞員時,本地主機:9000。它應該給我一個用戶JSON回來,這是在我的路線文件只有暫時。相反,它吐出以下內容。安裝創建反應應用程序與快遞

<body> 
    <noscript>You need to enable JavaScript to run this app.</noscript> 
    <div id="root"></div> 
    <script type="text/javascript" src="/static/js/main.ce2f0561.js"></script> 
</body> 

設置

使用create反應的應用程式內Express連接。

我的文件夾結構是

--src React app lives in this

--server

- index.js

- express.js

- 控制器

- 路線

-- rs_notes.js 

rs_routes.js

'use strict'; 



    module.exports = function(router){ 
    const notesController = require('../controllers/cs_notes'); 

    router.route('/', function(req, res, next) { 
     // Comment out this line: 
     //res.send('respond with a resource'); 

     // And insert something like this instead: 
     res.json([{ 
      id: 1, 
      username: "samsepi0l" 
     }, { 
      id: 2, 
      username: "D0loresH4ze" 
     }]); 
    }); 
    }; 

express.js

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

const app = express(); 

const router = express.Router(); 
// Setup logger 
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] :response-time ms')); 

// Serve static assets 
app.use(express.static(path.resolve(__dirname, '..', 'build'))); 
require('./routes/rs_notes')(router); 
// Always return the main index.html, so react-router render the route in the client 
router.get('*', (req, res) => { 
    res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html')); 
}); 

module.exports = app; 

index.js

'use strict'; 

const app = require('./express'); 

const PORT = process.env.PORT || 9000; 

app.listen(PORT,() => { 
    console.log(`App listening on port ${PORT}!`); 
}); 

整個項目鏈接 - https://drive.google.com/file/d/0B35OQMkRo3KcSHlkeXdWVjVjc0U/view?usp=sharing

我的問題或懷疑是

  1. 我是通過路由器在一個正確的方式。我們以前在表達4之前通過這個 的方式傳遞應用程序?所以不知道這裏是否有相同的結構。
  2. 我可以通過點擊localhost:9000(服務器由配置的節點服務器命令運行)而不是郵遞員在瀏覽器中加載它。
+0

這是HTML服務,還是你看到這是文本?前者對我來說似乎沒問題。 – idmean

+0

它在瀏覽器中工作嗎? – Colin

+0

堅持....我無意中提交了它......讓我編輯並正確詢問 – kushalvm

回答

0

我能夠通過適當學習使用路由器並在這裏和那裏移動一些代碼來修復這個堆棧。但它仍然不適用於基礎路由,即當我簡單地執行router.get('/',...)時。給出相同的錯誤信息。所以我寧願改變連接節點和反應的方法。出於與兩個單獨職位相同的原因,我在媒體上發表了我的努力。

https://medium.com/@kushalvmahajan/i-am-about-to-tell-you-a-story-on-how-to-a-node-express-app-connected-to-react-c2fb973accf2

相關問題