0

I'm doing some tests with React and Express. The final goal is to get Express to connect to a Mysql database and get data from it.

I understand how both React and Express work fairly well, and after a few tries I've been able to get my React app to get data from Express. This was done by running React on port 3000 and my server.js on port 8080 by following this guide.

What confuses me now that I've got this to work locally, is how am I supposed to get it to work after building my app.

I built it and uploaded it to check if it worked, but of course it didn't.

http://creativiii.com/build/

If you check the console you'll see it's not able to contact /ping.

Does express only work locally? I'm having a hard time figuring this out.

Here's my scripts just in case:

App.js:

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

class App extends Component {

  componentDidMount() {
    fetch('/ping')
      .then(response => response.json())
      .then(posts => console.log(posts))
  }

  render() {
    console.log(process.env.PORT)
    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>
      </div>
    );
  }
}

export default App;

server.js:

const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.get('/ping', function (req, res) {
 return res.send(JSON.stringify({ a: 1 }));
});

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(process.env.PORT || 8080);

Edit: I wrote the wrong title, fixed it now.

lpetrucci
  • 1,285
  • 4
  • 22
  • 40

1 Answers1

0

Frontend: Generally in React or Angular you can make use of Environment Variables present and set ur API globally only once so even if you change the backend port changing one line of code makes ur app to work again

scripts: {
    "dev": "webpack --env.API_URL=http://localhost:8000 --config webpack.config.dev.js",
    "build": "webpack --env.API_URL=https://www.myapi.com --config webpack.config.build.js"
  }

Backend: Coming to backend part if you are deploying the application on IIS server you need to do a lot of workout, if u use some Ubuntu it makes deployment easy and you need not change a line in your Express Code pasted Above. In case of Ubuntu you can just run node index.js and use some PM2 package for running the app in background

https://www.npmjs.com/package/pm2

Hope your doubt gets solved, if not you can comment below

Gvs Akhil
  • 2,165
  • 2
  • 16
  • 33
  • I'm deploying this on a VPS with Ubuntu. Should I upload my whole app folder, including "node_modules"? I tried to run my server.js file with node but nothing happens. I currently have only uploaded my "build" folder. – lpetrucci Oct 27 '18 at 16:39
  • Ya u need node_modules folder too. Server.js is ur main file? – Gvs Akhil Oct 27 '18 at 16:45
  • server.js is the file using Express. Should my node_modules folder be over 200MB? Seems awfully big – lpetrucci Oct 27 '18 at 16:50
  • 200mb?? what did u install? I have my node.js folder with just some 20mb which give some 50 API's for my application – Gvs Akhil Oct 27 '18 at 17:12
  • Just react and express. According to [this](https://stackoverflow.com/questions/21259181/how-to-package-deploy-node-js-express-web-application) I have to run npm install, but even after running it I still cannot run node server.js – lpetrucci Oct 27 '18 at 17:19
  • I don't think so, this is a new folder I created following the guide [here](https://github.com/facebook/create-react-app), then I ran [this](https://expressjs.com/en/starter/installing.html) installation. – lpetrucci Oct 27 '18 at 17:26
  • No, nothing at all – lpetrucci Oct 27 '18 at 17:27
  • Node is installed by the way, just checked by running sudo apt-get install node – lpetrucci Oct 27 '18 at 17:28
  • Can u just attach a pic of running node server.js in ur VPS – Gvs Akhil Oct 27 '18 at 17:29
  • I have moved this conversation to chat https://chat.stackoverflow.com/rooms/182648/room-for-gvs-akhil-and-eight – Gvs Akhil Oct 27 '18 at 17:31