NODE 2 explaining the fetch.

  • 1

  • 1

  • 1

The SSL (secure socket layer) is a security protocol that establishes an encrypted link between the server and the browser, to keep the data transfered secure.

The nodeJs v17 fixes a security hole of SSL, it requires to update react-scripts on old create-react-app apps.

//For this error
"error:0308010C:digital envelope routines::unsupported"

//Update it to the latest
npm i react-scripts@latest

The cross-origin fetch requests, sent from the ReactJs client to the NodeJs server.js, may be blocked due to browser security restrictions.

We add a middleware to allow the content-type methods.

//The CORS config handles it automatically
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin','URLs to trust of allow');
  res.header('Access-Control-Allow-Methods','GET, POST, OPTIONS, PUT, PATCH,DELETE');
  res.header('Access-Control-Allow-Headers','Content-Type');
  if ('OPTIONS' == req.method) {
      res.sendStatus(200);
  } else {
    next();
  }
});

On the ReactJs client we set the fetch configuration object for our own server.js route. Check React-3 for more API fetch().

The server.js receives and decostructs the request.body, we update the postSQL table and we response.send() the result.

The response.send() data will be treated as an object, so do not return strings.

On ReactJs we check the response object by using the status, statusText, and ok properties.

Deploying a full-stack app on the web

We configure a PostgreSQL database instance and connect to it locally using DBeaverarrow-up-right, employing the provided database name, username, and password.

From Render to DBeaver

We deploy a server.js web service to handle backend logic.

How we import a repository from github

This service connects to the Render-hosted PostgreSQL database using its internal URL, designed to minimize latence between services deployed on Render.

The client-side application can be hosted anywhere. It communicates with the backend by making API calls to the URL of the deployed server.js web service.

connect on server.js render web service

Private data on .env files

The .env file contains environmental variables, used to store configuration data, they are specific to the environment the app is deployed from.

The npm install dotenv module allows us to access the .env data.

We include the .env file in the .gitignore, to avoid committing the data in Github.

We use an environmental variable to allow Render to set up its own Port.

Render allows secure injection of .env data for sensitive information, either through Render's variable option (without "") or by importing/pasting the .env data directly (using "").

ENV database variable

1

Last updated