- create and clone github repository
- create a directory for client and another for server or setup the frameworks for each
mkdir my_projectcd my_projectnpx create-next-app@latest .- initializes a new Next.js project in the current directory
- setup the frameworks for each
- Install PostgreSQL on the official site
- You may need to use
sudo postgresql-setup --initdb. Withoutsudo, it sometimes doesn't work. - You may need to edit the
.conffile:sudo nvim /var/lib/pgsql/data/pg_hba.conf- On
pg_hba.confreplace:local all all ident- with:
local all all trust
- Restart PostgreSQL service:
sudo service postgresql restart - Access PostgreSQL CLI as postgres superuser:
sudo psql -U postgres
- When creating a Pool of connection on the server, you might need to change
pg_hba.conffile
- If you get the error:
[nodemon] starting `node server.js index.js` listening on port 5000 error: Ident authentication failed for user "postgres" - You might need to edit the
pg_hba.conffile:sudo nvim /var/lib/pgsql/data/pg_hba.conf- Replace "ident" with "md5" or "trust"
- I replaced with md5 on the todo pern project
- Create backend folder.
cdinto backend folder:
npm init -ynpm install express
- Create
server.js. - Import modules:
-
const express = require("express"); // Express "summarizes" the code for http requests
dotenv: reads.envfile and stores the environment variables that are confidential. Don't push it to GitHub.-
const app = express(); // Create an instance of an express application
- Use middlewares (
cors()andexpress.json()):
- Middleware: In Express.js, middleware functions are functions that have access to the request (req) and response (res) objects in the request-response cycle. They can perform tasks like parsing the request, modifying the response, or passing control to the next middleware in the stack.
app.use(cors()): allows requests from different domains.- In the code you provided,
const cors = require('cors');is used to import the "cors" middleware for an Express.js application. The "cors" middleware stands for Cross-Origin Resource Sharing and is used to enable or control cross-origin requests in a web application. Let me explain what it does:
- In the code you provided,
app.use(express.json()): allows the interpretation of JSONs by making them available as JavaScript objects.
- Write API:
- List an instance of a table:
app.<http_verb>("<endpoint path>", async (req, res) => { })- Try:
const results = db.query("<SQL query>");runs a SQL query viadbobject. console.log(results);res.status(200).json: send a response to the machine that originally made the request with the code of the operation (200) and the results in JSON format.
- Try:
- Create RDS instance:
- Choose PostgreSQL.
- Make sure security groups are enabled (which ports?).
- Define password username.
- Open DB manager.
- Start new connection:
- Choose PostgreSQL.
- Fill up credentials according to the provided image (not included in Markdown).
- useState: Update state of a variable.
const [todos, setTodos] = useState('');- Returns an array with two elements: the variable and a function to update it.
- Initial value is an empty string.
- useEffect: Manage component lifecycle, which includes:
- Create component
- Update component
- Delete component
- Event types: User events, keyboard events, mouse events, asynchronous events, etc.
- Examples of events:
onClick,onChange,onSubmit,onMouseOver/onMouseOut,onKeyPress/onKeyDown.
import React, { Fragment, useState } from "react";
const <component_name> = () => {
const [attribute, setAttribute] = useState(<object.attribute>);
};