Coder Social home page Coder Social logo

react_express_app_medium_tutorial's Introduction

React_Express_App_Medium_Tutorial

  • This repository has the code to support a tutorial that I created on Medium called Create a React FrontEnd, a Node/Express BackEnd and connect them together.
  • This repository can also be used as a starting point (boilerplate), if you whant to create your own React/Express app. .

Where can I check the tutorial?

You are very welcome to check the tutorial on Medium. Just follow the link below.

About the app

Actually, there are two separated apps. The Client which serves the FrontEnd (using React), and the API (in Node/Express).

How to run the API

  1. In your terminal, navigate to the api directory.
  2. Run npm install to install all dependencies.
  3. Run npm start to start the app.

How to run the Client

  1. In another terminal, navigate to the client directory.
  2. Run npm install to install all dependencies.
  3. Run npm start to start the app

Check if they are connected

  1. With the two apps running, open your browser in http://localhost:3000/.
  2. If you see a webpage saying Welcome to React, it means the FrontEnd is working.
  3. If the same webpage has the phrase API is working properly, it means the API is working.
  4. Enjoy!

react_express_app_medium_tutorial's People

Contributors

joao-henrique avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react_express_app_medium_tutorial's Issues

Issue 1: Someone made an oopsie woopsie

A certain someone (almost certainly from this paper) pull requested to the original branch made by Joao Henrique!
yikes
blame GitHub for the bad UI (why does it default to that branch and not your own fork?)

Minor edit for app.js in client directory

Hi,

I'm suggesting a minor edit for the code in app.js in client directory as I tried the tutorial that you have set up.
Please let me know your thoughts. Thank you

class App extends Component to be class App extends React.Component instead for it to work.

import React from 'react';
import logo from './logo.svg';
import './App.css';

class App extends React.Component {
constructor(props) {
super(props);
this.state = { apiResponse: "" };
}

callAPI() {
fetch("http://localhost:9000/testAPI")
.then(res => res.text())
.then(res => this.setState({ apiResponse: res }))
.catch(err => err);
}

componentDidMount() {
this.callAPI();
}

render() {
return (



logo

Welcome to React



{this.state.apiResponse}



);
}
}

export default App;

Liscense

Hi there!

I was wondering if you could add a public use license to this so that we could use it for a hackathon we are participating in. Here is the link to the license format if you're unfamiliar ( https://opensource.org/licenses/MIT )

Thank you!

Had to rearrange Client/App.js file

Others might run into this problem so I figured I might as well post the code inside the Client/Apps.js file that I used to get things working.

If you're starting a new React project, you're going to start with a function inside Client/App.js called, App(). This needs to be replaced by Render() and moved into App class. Please see below:

import React, {Component} from 'react'
import logo from './logo.svg';
import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { apiResponse: "" };
  }

  callAPI() {
      fetch("http://localhost:9000/testAPI")
          .then(res => res.text())
          .then(res => this.setState({ apiResponse: res }));
  }

  componentDidMount() {
      this.callAPI();
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
        <p className="App-intro">{this.state.apiResponse}</p>
      </div>
    );
  }
}

export default App;

Outdated packages and dependencies need update to fix critical issues

Just a notice you should update the package.json of api and client to newest versions to be able to run it without critical vulnerabilities

API's package.json:

{
  "name": "api",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "^1.4.5",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "express": "^4.16.4",
    "http-errors": "~1.6.2",
    "morgan": "^1.10.0",
    "pug": "^3.0.0"
  }
}

Client's package.json:

{
  "name": "my_react_app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "^3.4.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
``

Doing away with the react web server altogether

Joao-Henrique, thanks for the good explanation.

I have not yet started on react, but have some experience with traditional web applications using Node Express.

I am curious about that web server created by create-react-app. What technology is it using and how much resources is it using? As react is a single page app, why do we have to set up a different web server just to serve that page? Is it possible or easy to move that page to just another route in my Express server?

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.