Once a table has been created, if changes need to be made, they need to be made in the js but also in the SQL database (through the terminal). Two examples below
ALTER TABLE clicks ALTER COLUMN name TYPE VARCHAR
ALTER TABLE clicks ADD COLUMN userId VARCHAR
Building an API:
In terminal -> yarn add express (this adds new dependencies to project)
To access express, go to index.js:
const express = require(‘express’);
const app = express();
app.listen(3000, () => console.log(‘something’));
app.use(express.json()); <- this line of code allows us to read incoming json requests
Get requests usually have (_request, response) whereas for patch, and post we use (request, response)
For get requests, we don’t actually use request, we just need response, if you are not using it yet and you want eslint to not point it out, add _ in front of request)
For other requests, when users send us information (patch/post) those are considered requests
json() is the standard, if I want to add a string inside the json, I can just use “”, but if I want to pass through an object, I need to add a second set of brackets {} inside () to ensure it is valid json (the outer set of round brackets are needed no matter what)
Additional Notes:
node.js is used when we code in JS but not in a browser
To store information locally use yarn add dotenv
require(‘doting’).config() <- add to index.js (add this as early as possible in the code)
This is where we want to store any secret information
Make sure to add .env file into .gitignore so that it will not get pushed up to GitHub
app.listen turns the server on
app.use(express.static(‘public’)) - in your public folder you can add other files which will create your frontend
use express.json() reads incoming json requests from the frontend
SQL tips:
Building an API:
Additional Notes: