javascript - Setup create-react-app with Express -
problem - not able response postman when hitting localhost:9000. should give me user json in routes file time being. instead spits out following.
<body> <noscript>you need enable javascript run app.</noscript> <div id="root"></div> <script type="text/javascript" src="/static/js/main.ce2f0561.js"></script> </body>
setup
using create-react-app express connect.
my folder structure
--src react app lives in this
--server
-- index.js
-- express.js
-- controllers
-- routes
-- 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 line: //res.send('respond resource'); // , insert 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); // return main index.html, react-router render route in 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}!`); });
full project link - https://drive.google.com/file/d/0b35oqmkro3kcshlkexdwvjvjc0u/view?usp=sharing
my questions or doubts are
- am passing router in right way. used pass app in way prior express 4 ? not sure if same structure works here.
- i able load in browser hitting localhost:9000 (server run node server command configured) not in postman.
i able fix stack learning use of router appropriately , moving code here , there. still not working base route i.e when router.get('/', ...). gives same error message. rather reversed approach of connecting node , react. published efforts on medium same reason 2 separate posts.
Comments
Post a Comment