View WebApp: https://newsheadlines-c0haf4f6d4bvhaa7.westeurope-01.azurewebsites.net/
Contents of this Repo:
- Make use of tailwind for CSS
- Fetch API from www.newsapi.org and display in application.
- Create and Fetch a Local Host Server API.
Document Object Model (DOM) Tree of HTML Objects Each element is a node in the tree.
document.getElementById(id) document.getElementByClassName(class name) - Returns HTMLCollection document.getElementByTagName(tag name) - Returns HTMLCollection document.getElementByName(name) - Returns NodeList
element.querySelector(selectors) - Returns NodeList document.querySelector(selectors) - Returns NodeList element.querySelectorAll(selectors) - Returns NodeList document.querySelectorAll(selectors) - Returns NodeList
document.createElement(‘tagName’) variable.textContent = “text” document.appendChild(variable)
Element p Class .example ID #header Attribute [type=”text”] Descendant div p Child ul > li Adjacent Sibling h2 + p Pseudo-classes and elements :hover or ::before
addEventListener(‘type’, callback function)
for ( let i=0 ; i < x.length ; i++ ) { return i }
array.find( function ( x ) { return x === 0 }) : Search JSON to find object.value array.map( function ( x ) { return x * 2 }) : Search JSON to return object.values forEach( function ( x, index, array ) { x += 2 }) : Iterate JSON for side effects
( ) => { } array.map( ()=>{} ) :Map with a empty callback
Const addArrow = ( a,b ) => a+b; Arrow Function Const add = function( a,b ) { return a + b }; Normal Function
this
and call
for Objects
const x = {value:0,
printValues: function() {
console.log(this.value)}
x.printValues() : outputs 0
const y = {value: 5}; x.printValues.call(y); : output 5
const candyBag = { food: ‘bread’, drink: ‘water’}
const {food, drink} = candyBag; console.log(food);
Object.Freeze() NB: Does not apply to nested objects.
Const Constants = Object.Freeze({ PI: 3.14159 })
localStorage.setItem(‘key’,’value’) :Store const value = localStorage.getItem(‘key’) :Retrieve localStorage.removeItem(‘key’) :Remove
.trim() :Remove whitespace from both ends of string
Fetching APIs (Fetch or axios or XMLHttpRequest)
fetch().then().then().catch() : Promise Chain Example
fetch( url,options ) .then( ()=>{} ) .catch( ()=>{} )
axios.get( url ) .then( ()=>{} ) .catch( ()=>{} )
fetch( url ) .then( response => { if( !response.ok ){ throw new Error( ‘Network response was not ok’ ) return response.json() }) .catch( error => { console.error( ‘Error: error’ } )
const express = require(‘express’) :Import Express Framework const app = express() :Create Express App const port = 3000 :Port for the server to listen
app.use( express.json() )
app.get(‘/api/hello’, (req, res) => { message: ‘Hello’ } )
app.listen(port, () => { console.log( ‘Server Running’ ) } ) : Terminal - node server.js