score:3
Yes, React runs on the client and Express is a Node.js framework. There's a pretty good chance you're using Express if you're running any boilerplate.
Here's a pretty good walkthrough on more complete routing. https://medium.com/@patriciolpezjuri/using-create-react-app-with-react-router-express-js-8fa658bf892d
In several of my applications my routes look something like this:
//router.js--and I'm positive this is from some react-express boilerplate
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
const react = (req, res, next)=>{
res.render('react', {
title: 'React Application',
layout: false
});
};
router.get('/app', react);
router.get('/app*', react);
module.exports = router;
//app.js
...
app.use('/', routes); //<--this is the exported router.
...
If you want to be more simple it is probably something like:
let reactRoute = (request, response, next) => {
//render your react page however you're doing that.
}
express.use('/API', yourApiFunction)
express.use('/', reactRoute)
express.use('/*', reactRoute) //Wildcards are REALLY important if you're routing inside react.
You can also bypass things with a proxy but that tends to get more complex than you probably want. Also--keep in mind you don't have to stick to Node on the back-end if you're not comfortable with it. React is client side, I use it with a few production .NET apps, some PHP (lordy!), and, yes, a lot of Node servers.
score:0
to solve No 'Access-Control-Allow-Origin' header is present on the requested resource.
you've to use the cors middleware
go to the term
yarn add cors
or npm i cors
in server.js
const cors = require("cors");
const express = require("express");
const app = express();
app.use(cors());
Source: stackoverflow.com
Related Query
- What is the general practice for express and react based application. Keeping the server and client code in same or different projects/folders?
- Host React and Express on the same server?
- How can I start my node server and react app at the same time?
- Eslint for a express and react project in the same project
- Deploying React (3000) and Express (8000) app on the apache server
- Can I use mock data and proxy api data at the same time in my react project when using node express as a proxy server?
- Hosting React FrontEnd and NodeJs Backend on the same Server
- How to host a static landpage and react on the same node server?
- "How to fix Failed to load resource: the server responded with a status of 500 in React and express
- Connect express backend to React frontend (in the same server if possible)
- how to share environment variables between react app and express js server hosting it as static site
- MERN Stack - Express and React on same port?
- Deploying react app on github pages front and backend in the same repository
- Post an object with fetch using react js and express API server
- Using express and es6 to render react and jsx server side
- CORS issue with Django and React hosted on same server
- How to use the same port for React Js and Node Js?
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- In React componentDidUpdate, can props and state change at the same time?
- Is there a clean way to conditionally load and render different components for the same React Router route?
- Upload a file in React and send it to an Express server
- How to use currying to create HoC in React and connect to the Redux store at the same time?
- React Router v5 accompanied with Code Splitting, and Data Prefetching with the use of Server Side Rendering
- React/webpack - How can I host react app on one server and images/fonts on another server?
- React and React Router, rendering the same element twice with a different prop results in two elements with the same prop?
- Is it possible to run Node and React in the same port?
- React native picker and console showing different value of the same state
- How to Fetch and Display an Image from an express backend server to a React js frontend?
- React Router List and Detail Route at the same time
- how to run my node application and react app in the same time with one command?
More Query from same tag
- Which is better CRUD coding style using Redux?
- eslint plugin fp, jest and react testing library causes unwanted linting errors
- React onClick not working in any of my browsers, but for colleagues it does
- react-bootstrap + purgeCss + next.js
- Getting data from metaweather API to react page
- How to change background color of button using react
- How to style react-select component so that it is inline with some text?
- calling setState in an arrow function not altering the state?
- Is there anything wrong with passing a function that returns a component as a prop?
- React: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL
- Eslint import/no-extraneous-dependencies error on path
- Notification when user try to close browser or tab
- Get the ant-design Select dropdown value when hover
- Nginx config to serve react app from sub-folder
- How to lazy load route with react location?
- How can I add computed state to graph objects in React Apollo?
- Gatsby Replace Static Query Data at Runtime
- React | NextJS : Cannot use props returned by getInitialProps()
- Reactjs app doesn't show image preview in safari
- ReactJs - Does not render the updates after setState is called
- React component issues with state, setState and render
- only onChange and value for current input in a mapped array
- How can I recursively update object properties in hook?
- Fetching fallback url if the first url fails
- Why useContext does't work in my function?
- update state from child in React
- How to to cancel pending asynchronous actions in React/Redux
- How to sort inbound props in a ReactJS component?
- Downloading Large Number of Files in Frontend using jszip
- How to prevent to enter space/whitespace in password input field | ReactJs