400 Bad Request ( Node.js )

Hello
I’ve deployed my back-end Node Express server as a Web Service on Render. It is supposed to direct queries to a PostGreSQL database that is also set up on Render. The front-end (Create-React-App) is hosted on another site.
The database is used to store simple login credentials (name, email, password) and the server is supposed to check credentials to allow access to other pages.

I’ve set up the fields for Environmental Variables in my Settings, where I entered Key/Values for password, host, user, database etc.
I’m using Knex.js so I can use javascript syntax to make postgreSQL queries. Here is the code block in my app that incorporates the Environmental Variables to make queries:
const db = knex({ client: 'pg', connection: { host : process.env.HOSTNAME, port : 5432, user : process.env.USERNAME, password : process.env.DB_PASSWORD, database : process.env.DATABASE, ssl: true } });

app.get('/',(req,res)=>{ db.select('*').from('users') .then(data =>{ res.json(users) }) })

app.post('/signin',(req,res) => {signin.handleSignin(req,res,db,bcrypt) });

app.post('/register',(req,res) => { register.handleRegister(req,res,db,bcrypt) });

I’m using a fetch() statement to make the API call from the front-end. Here’s an example:
fetch('https://smartbrain-api-e1it.onrender.com/register', { method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ email: this.state.email, password: this.state.password, name: this.state.name }) })

My issue is that whenever I click ‘Signin’ or ‘Register’, I get a “400 Bad Request” in the browser console, and nothing happens.
I’m rather new at this, and not sure how everything is supposed to work. Is there some specific syntax to pass on credential queries to the database?
What could be the cause of my problem?
How can I get more specific feedback or response from my back-end?

Thanks and any help is appreciated!

Hi
I’ve solved my issue:
I needed to change the syntax when addressing the Environmental Variables within my ‘server.js’ script.
Here is what I used with success:
const db = knex({ client: 'pg', connection: { connectionString: process.env.DATABASE_URL, host : process.env.HOSTNAME, port : 5432, user : process.env.USERNAME, password : process.env.DB_PASSWORD, database : process.env.DATABASE, ssl: {rejectUnauthorized: false} } });

I referenced this article:

Hi there,

Thanks for sharing your solution with the community.

Kind regards

Alan

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.