An online markdown blog and knowledge repository.
Skipped for today due to workload.
in class solution included:
recipe.ingredients.forEach((ingredient) => { let withoutAmount = ingredient.slice(ingredient.indexOf(' ') + 1); let withoutUnits = withoutAmount.slice(withoutAmount.indexOf(' ') + 1); results.push(withoutUnits); });
the indexOf(' ') returns the idx of the space character
RegEx: Regular Expression
Not very intuitive.
Variety of uses but gist is:
Pattern is used to compare if all or part of a string matches the patterns.
RegEx is very literal, so lower-case vs upper-case matters, etc.
Returns true or false: regex.test(str)
Returns an array if there are matches, or null: str.match(regex)
Returns a new string with the matches replaced by the provided replacement: str.replace(regex, replacement)
Flags are used
Global: 'g' => Don't return after first match, run against entire input.
Multi Line: 'm' => Single-line test.
Case insensitive: 'i' => Does not pay attention to case.
Sheyna used ReplIt and regex101, both will be linked to the class repo.
let regex = /pattern/;
let result = regex.test('test string');
If regex does not find any matches it returns null.
Use a Logical-Or to evaluate a null return to an empty array instead: var result = str.match(regex) || [];
Greedy: Selects one or more matches between one and unlimited times as many times as possible, giving back as needed.
Abdulahi strikes again!
Notes:
Remember to add the variable REACT_APP_SERVER=http://localhost:3001
to the FE .env.
When cloning other code, add a .env file and set the server listening port and restart the server.
Error Handling: Some types of error conditions will not trigger a try-catch (probably because it is happening outside of it).
Require the dotenv file with require('dotenv').config()
.
dotenv is necessary to 'pick up' stored variables from a flat file.
If server is unable to access dotenv file or the PORT value within it, then the server will be on the 'alternate port' e.g. 3002.
Express.js is a node-based Server using javascript.
CORS must be requied so the server can share with multiple computers.
CORS is 'middleware'.
Putting multiple API calls within a single try-catch within a single method is frowned upon, instead:
Concatenate the URL.
Get the Path to the image.
Make the request for the weather data.
Axios, CORS, Express exercise!
Use our existing front-end application.
Create a backend that will find items on the internet and then send them back to the requesting front-end client.
Steps
Different this time.
npm init -y
. Verify that the resulting package.json file is correct especially "start" and "main", so that nodemon will work.npm i express cores dotenv axios
. Yes, axios too!API Key.
End Point URL and the VERB i.e. 'GET'.
The parameters (required, optional, and exclusive) to make a valid request.
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const axios = required('axios');
const app = express(); // express server
app.use(cors());
const PORT = process.env.PORT || 3002;
// root route
app.get('/', (req, res) => {
res.status(200).send('Ehlo owrld.');
});
// a route to handle searching for photos
app.get('/photos', (req, res) => {
// see the class repo for the notes
// basically: process the request => send an axios =>
// ...process axios reply => format data into a class instance =>
// ...use res.status(nnn).send(processed_data)
});
// catch-all route MUST BE LAST
app.get('*', (req, res) => {
res.status(404).send('Nothing here');
});
app.listen(PORT, () => console.log(`Listening on port: ${PORT}`));
app.use((err, req, res, next) => {
console.log(err.message);
res.status(500).send(error.message);
});
// server-side
class Photo {
constructor(pic) {
this.src = pic.urls.regular;
this.alt = pic.alt_description;
this.artist = pic.user.name;
}
}
A VSCode extension.
Install it GLOBALLY.
Use it to make REST API calls.
heroku login
(it helps to already be logged in to the website on the same workstation).git push heroku main
git push heroku main
.FE makes axios call to BE Server.
BE Server captures call, takes data, makes an Axios call to remote API.
Remote API response is transformed by our BE Server then responds to FE.
FE server processes response to display results.
Use next section of Trello Board.
Make two calls to two APIs, to get real data from WeatherBit.io API.
RegEx 101 - Use ECMA Script setting
MDN RegEx in javscript
Unsplash API