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

  1. am passing router in right way. used pass app in way prior express 4 ? not sure if same structure works here.
  2. 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.

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


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -