2017-07-26 64 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

相关问题