title: How to link Gatsby.js with my Express server
body: <p>I am trying to make a very basic full-stack application where the front-end is Gatsby and the Backend is a simple Express server. I have a Database where I have some users and the goal is get these users in the backend with a query and then displaying them in the Gatsby (React) component using fetch(). Obviously something goes wrong and I cannot display them on the screen, even if using Postman the request is processed correctly.
  Here are the relevant code pieces:</p>
  
  <p>Express: I used the classic express-generator and I only changed the route file users.js, to link it to the database. (since Gatsby uses the port 8000 there is no need in changing the port so that React and express do not overlap).</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  const mysql = require('mysql');
  
  var mysqlconnection = mysql.createConnection({
    host     : ****,
    user     : ****,
    password : ****,
    database : ****
  });
  
  mysqlconnection.connect(function(err) {
    if (err) {
    console.log(err)
    }
    else {
    console.log(`You are now connected` )
    }
  })
  
  router.get('/', (req, res) =&gt; {
    mysqlconnection.query('SELECT * FROM table_basic', (err, rows, fields) =&gt; {
        if(!err) {
            res.send(JSON.stringify(rows, undefined, 2));
        }
        else {
            console.log(err)
        }        
    })
  })
  
  
  
  module.exports = router;
  </code></pre>
  
  <p>Gatsby page: this is NOT the main index.js file, it is a secondary page coded as contacts.js in the page folder (Gatsby uses a built-in routing method). The component is wrapped around the layout (footer and header, working like the hbs partials).</p>
  
  <pre><code>import React, { Component } from 'react';
  import './contacts.css';
  import Layout from '../components/layout';
  
  export default class About extends Component {
      constructor(props) {
          super(props)
  
          this.state = {
              users: []
          }
      }
  
  
      componentDidMount() {
          fetch('/users')
            .then(res =&gt; res.json())
            .then(users =&gt; this.setState({ users }));
        }
  
  
  
    render() {
      return (
          &lt;Layout&gt;
              &lt;h1&gt;LISTING&lt;/h1&gt;
              &lt;ul&gt;
                {this.state.users.map(user =&gt;
                  &lt;li key={user.id}&gt;{user.lastname}&lt;/li&gt; 
                  )}
              &lt;/ul&gt;         
          &lt;/Layout&gt;      
      )   
    }
  }
  </code></pre>
  
  <p>And, finally, to link the back-end with the front-end, I added <code>"proxy": "http://localhost:3000",</code> in the Gatsby package.json to point to the Express server. Everything works fine there, using Postman and wherever I try to fetch the data, but they do not display on the screen. Any ideas? </p>