Sorry for the ignorance here -- new to the game. I am attempting to deploy a PERN app to Bluehost. I'm using react-router to serve static pages on the front end, this is working fine. To get this to work, I had to edit the .htaccess as below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
I am attempting access a pg database on the front end by serving via Express. I've downloaded Node to the server via SSH and know I am able to access my 'users' database by just logging to the command line. My simple router looks as such:
const express = require('express');
const { Pool } = require('pg');
const app = express();
const port = 3000;
const pool = new Pool({
user: "proofrm4_admin",
host: "localhost",
database: "proofrm4_main",
password: "******",
});
app.get('/users', async (req, res) => {
try {
const users = await Pool.query('SELECT * FROM users');
return res.status(200).json({data: users.rows});
} catch(err) {
return res.status(400).json({error: err});
}
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
My front end fetch request is as such:
import NavBar from '../components/Navbar';
const getUsers = async () => {
try {
const users = await fetch('http://localhost/users', {
method: 'GET'
});
return await users.json();
} catch(err) {
return {err};
}
};
const users = await getUsers();
const HomePage = () => {
return (
<div>
<NavBar/>
{console.log(users)}
</div>
)
}
export default HomePage;
I've tried all sorts of cors settings and have also played with adding a proxy at localhost:3000 to the package.json file. Despite this, I can't get the Express server to properly serve the database information. Is there something else I need to add to the .htaccess? Do I need a different proxy? Is there just an easier way to go about this completely? Thanks in advance for the help!