Giter Site home page Giter Site logo

nodejs-reactjs's Introduction

Learn to combine NodeJs and ReactJs

How it works

The Idea from a high level prespective is pretty is Simple, what you need is:

  1. A Backend API to serve data to your react app.
  2. A React app to request said data while also doing read/write opration to your database through your api.

to make the Idea even more simple your project folder should look something like this.

-Backend(API)

  • node_modules
  • index.html
  • package-lock.json
  • package.json
  • server.js
  • router

-Frontend(React App)

  • node_modules
  • public
  • src
  • index.html
  • package-lock.json
  • package.json

somethings to take note of is the way to give and receive data from your backend and frontend respectively is by

On the backend you will use the library cors this Library does Cross-origin resource sharing to expose your express API to download the package type this in your terminal npm install cors. then you need to use it as a middleware, To do so you need to enter this line after you instantiate your express app (after you import the package) app.use(cors()).

On the frontend you can use the built in fetch API or for more flexibility use the axios package. to download the library you need to do the same thing in your react app (remember to switch your terminal from the backend to the frontend so you don't download the package in your backend). to install npm install axios and to fetch data read the axios docs. but for a simple demonstration for how to do get and post requests follow the examples below

to do a get request

axios.get('https://example.com/')
.then(response => {
    return response.json()
})
.catch(err => console.log(err)) 

to do a post request

axios.post('https://example.com/', dataToSend)

try to clone and run the code to see how it works (remember that you need to install the packages for both the backend and frontend servers) I would advice following this tutorial since it does a good job explaining what you're doing if this wasn't clear enough.

and thats all for today have a good one ;)

nodejs-reactjs's People

Contributors

mohmmad-ow avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.