6 call up localhost:8000 and view the data
7 after playing around with json on a local server we can then create a public S3 bucket in AWS

Configuring an AWS S3 Bucket as our Public Server

The simplest way to store a JSON file remotely is in AWS S3. By not creating a schema in a traditional data server we become serverless. We are out in the AWS cloud with S3 where we can connect to our bucket link from anywhere. As said previously the noSQL S3 approach has some limitations. But it also has big benefits. When working with data, human nature tends to want to only use one table similar to the ole days where we relied heavily on one excel table. This one flat file format can handle a few metrics so we don’t bombard our audience with complexity. An ideal use case for a flat file is a simple dashboard.

First we create an AWS s3 bucket then we upload the JSON file. Here is how we do it:

1 Sign up for free tier AWS, go to S3 from the AWS console and create a unique bucket name

2 make it public, by going to bucket permissions tab to turn off block public access - go to block public access section, edit, uncheck and save

3 stay in the permissions tab then edit bucket policy, erase what is there and replace with following, then save changes (remember to replace your bucket name below with the actual name)

{\\\"Version\\\": \\\"2012-10-17\\\",\\\"Statement\\\": [{\\\"Sid\\\": \\\"PublicReadGetObject\\\",\\\"Effect\\\": \\\"Allow\\\",\\\"Principal\\\": \\\"*\\\",\\\"Action\\\": \\\"s3:GetObject\\\",\\\"Resource\\\": \\\"arn:aws:s3:::your bucket name/*\\\"}]}

4 then, while still in the permissions tab go to cross origin (cors), edit and replace with below and save

[{\\\"AllowedHeaders\\\": [\\\"*\\\"],\\\"AllowedMethods\\\": [\\\"GET\\\",\\\"HEAD\\\"],\\\"AllowedOrigins\\\": [\\\"*\\\"],\\\"ExposeHeaders\\\": []}]

5 Go to the properties tab and press static web hosting, the last item. Then click edit then enable then save. This transforms your bucket link into a public website.

6 Go back to the objects tab and press Upload, then go to where u saved ur newly created JSON file and complete this operation. Your json file will now be in s3.

7 Once the JSON file is uploaded successfully it will appear in the objects section of ur s3 bucket. Double click the JSON file and u will be in the properties tab where u see Object overview. Click on the object url provided and raw JSON data will appear. Copy and save this url link somewhere because u will need it to fetch data from Javascript. This url link provides access to the newly created generic website, a publicly accessible S3 bucket that serves as a temporary website. You can save files to this bucket and share them with others at any time.

8 accessing our json file from AWS requires different coding than used in a local server. First we need to provide the user with a link to our web page. My public link to our fictitious orders data is
https://rickd.s3.us-east-2.amazonaws.com/orders4.json Then our code should look like this which is different from our original code. For this code instead of displaying our array we add table elements so display is in table format

    

next, here is the same code where we add headers and styling

          

Part 2

Arrays, Loops and Functions, some building blocks of coding, the bedrock essentials of programming

Array - the most important data structure
In javascript our data is structured as key-value pairs surrounded by curly braces and delimited by commas, an array contains multiple javascript objects with each object representing a row of data. This is called JSON format or Javascript Object Notation.

Looping / Iteration - the most important loop is the for loop but u should study up on other types of loops. A for loop iterates over an array of data and performs an operation for each pass. In our use case it just displays the data to the frontend via a fetch request.

a function
A function is a block of code, a set of instructions, that executes in response to some kind of event like pressing a button. In some cases functions are not necessary but in other cases a function is required for some code to work properly. Functions either have a name or are annonymous. They can be called or can be self executing. The dreaded arrow notation is intimidating at first but it is used widely so get used to it.

conditional logic last but not least we also have the if / else statement. if(condition) {then do this} else {do that}

in conclusion it is necessary to study up on all these topics to gain more knowledge as I am providing just a small synopsis here in this lesson. I am providing only a roadmap, a primer of sorts, and u as the programmer must learn the rest.

Happy coding !!

addendum

our plain js, nosql use case is a bar chart where u can find the code here....at https://dev.to/rickdelpo1/stacked-bar-chart-using-a-json-data-source-plain-vanilla-javascript-plain-css-and-no-chart-libraries-2j29

about the author Rick Delpo.. https://javasqlweb.org/about.html

","image":"http://www.luping.net/uploads/20241115/17316496886736e0983f2c5.jpg","datePublished":"2024-11-15T14:22:17+08:00","dateModified":"2024-11-15T14:22:17+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 簡單的 Javascript 複習,適合那些感覺落後或不知道從哪裡開始使用函數、陣列、循環、JSON 和 NoSQL 的人

簡單的 Javascript 複習,適合那些感覺落後或不知道從哪裡開始使用函數、陣列、循環、JSON 和 NoSQL 的人

發佈於2024-11-15
瀏覽:147

Plain Javascript Refresher for those feeling left behind or not knowing where to start w/ Functions, Arrays, Loops, JSON & NoSQL

Here we will learn Plain Vanilla JS without using NodeJS or external libraries. We will start right out of the box with a use case by converting some Google sheet data into JSON and storing it in an AWS S3 bucket, then fetching it using plain js. Most lessons start with a hello world program and not much else but here we actually have something to program right away so we can practice our arrays and loops which are key pillars in any programming language. Plus you can have a foray into the world of Data Science here and perhaps make a career out of it like I did.

When we first study data we think of SQL but there are many apps where SQL is overkill and not needed. In a dashboard with a few metrics we can get away with a simple JSON flat file as our data source with no relationships to other data. Dashboards can use this NoSQL format and are a popular choice for a Marketing groups reporting needs.

In order to set up our environment we only really need Google Chrome and the json chrome extension for converting spreadsheet data to json. Then we need free AWS and an S3 bucket as our generic website. For an IDE we will use just a windows notepad. We will also need a local network to fetch our data because fetching data from the C drive will not work as the javascript fetch api uses http protocol so a web server is needed for this. Before going public on free AWS we will set up a local web server first to test our data. We will use simple Python for this.

set up the environment
steps to set up a local Python server and index file

Before creating an AWS remote server we need to first set up a local web server using Python..here is how to do this

First find out if Python is installed... open a command prompt which will default to your home folder c:\Users\yourname and type in python. If version info shows then it is installed and u can go to step 6 below (but be sure that an index file is saved in your home folder first)
if you dont have python installed follow these instructions

Python Server in windows
1 go to search area and type cmd then hit command prompt, a black screen will open with path to your home folder (usually C:\Users\yourName)
2 type in python, if installed it will show a version number
3 if not installed then the get button shows up, press this and download will install over a few minutes (or just download python from chrome)
4 once fully installed reopen cmd prompt and type in python again
5 version info will show....here is where we start if python is already installed
6 type in python -m http.server and this starts server (keep this cmd window open)
7 be sure you saved an index file in home folder (in file explorer click c: then Users then yourName to open home folder)
7a keep cmd open while u go to localhost in step 8...closing cmd requires having to reopen and start over
8 go to chrome and type in localhost:8000 and your default index page will appear....see below for creating an index file

Python server on a Mac
on a mac open a terminal and start with step 2 above ....except might need to try 3 different options above depending on version of python that is pre installed. Our home folder should be the folder Python is installed in and the same as the terminal folder where we start server.

try this first

  1. type in python -m http.server or
  2. type python3 -m http.server if above does not work Hit return and Python 3 will instantly start a simple HTTP server from the directory in which the command was executed..this dir should also have an index file or option 3 if others dont work
  3. type in python -m SimpleHTTPServer 8000 for older versions

How to create an index (home) file in our Python path..save it to same folder where the web server resides. Copy this code and save as index.html

     

hi there, this is our first html page

prepare some data

1 copy this data and paste it into a blank google sheet
this is our fictitious company with an orders database

order_no,order_date,product_line,dollar_amt,product1,product2,product3
12340,01-03-22,prod1,400,400,0,0
12341,01-02-22,prod2,50,0,50,0
12342,1-16-22,prod3,50,0,0,50
12343,1-17-22,prod1,100,100,0,0
12344,1-15-22,prod2,50,0,50,0
12345,2-5-22,prod1,100,100,0,0
12346,2-6-22,prod3,20,0,0,20
12347,2-7-22,prod1,100,100,0,0
12348,3-23-22,prod2,200,0,200,0
12349,3-5-22,prod3,20,0,0,20
123410,3-29-22,prod1,100,100,0,0
123411,3-25-22,prod1,100,100,0,0
123412,4-23-22,prod1,500,500,0,0
123413,4-24-22,prod2,100,0,100,0
123414,5-10-22,prod3,50,0,0,50
123415,5-15-22,prod1,500,500,0,0
123416,5-25-22,prod2,50,0,50,0

VERY IMPORTANT - after pasting data and while it is still highlighted, in google sheets, press data then split text to columns

2 get the json chrome extension
enable chrome to save as json before creating the sheet.
I found this easy shortcut that adds a JSON icon to the google sheet toolbar...this is a chrome extension
first go to this link https://chromewebstore.google.com/detail/sheets-to-json/enmkalgdnmcaljdfkojckdbhkjmffmoa
then press add to chrome, over to the far right of page
then open a blank google sheet and u will see the JSON icon as the last item in the toolbar near top of page

3 transform your data into json
paste above data into the sheet, then text to columns, then press the json icon and go to downloads to get ur json file

4 save this json file in the same folder where python and your index file reside...I saved it as orders.json

execute our program

5 fetch data from your index file...testing our server and files config

change your index.html file to contain the following code which is different from the code we will use below to access the data from a public server


    
版本聲明 本文轉載於:https://dev.to/rickdelpo1/plain-javascript-refresher-for-those-feeling-left-behind-or-not-knowing-where-to-start-w-functions-arrays-loops- json-nosql-5cpp?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta 中的列偏移發生了什麼事?
    Bootstrap 4 Beta:列偏移的刪除和恢復Bootstrap 4 在其Beta 1 版本中引入了重大更改柱子偏移了。然而,隨著 Beta 2 的後續發布,這些變化已經逆轉。 從 offset-md-* 到 ml-auto在 Bootstrap 4 Beta 1 中, offset-md-*...
    程式設計 發佈於2024-11-15
  • 我們能否在正規表示式中實現真正的可變長度向後尋找?
    我們能否在正規表示式中實現真正的可變長度向後尋找?
    正規表示式的可變長度回顧斷言替代方案正規表示式中的可變長度回顧斷言,用(?具有正規表示式模組的 PythonPython 正規表示式模組提供可變長度後向斷言的支援。 import regex m = regex.search('(?<!foo.*)bar', 'f00bar') pr...
    程式設計 發佈於2024-11-15
  • 您應該為您的應用程式選擇哪種地理鄰近度公式?
    您應該為您的應用程式選擇哪種地理鄰近度公式?
    地理位置鄰近度計算:公式比較在開發地理位置鄰近度搜尋時,了解公式選項之間的細微差別至關重要。雖然大圓距離公式和半正矢公式曾經被認為是同義詞,但它們之間存在一些微妙的區別,這些區別會影響速度、準確性和效率。 公式比較用於地理計算的三個主要公式鄰近度計算為:1。半正弦公式:d = 2r * arcsin...
    程式設計 發佈於2024-11-15
  • 如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    如何在 PHP 中組合兩個關聯數組,同時保留唯一 ID 並處理重複名稱?
    在 PHP 中組合關聯數組在 PHP 中,將兩個關聯數組組合成一個數組是常見任務。考慮以下請求:問題描述:提供的代碼定義了兩個關聯數組,$array1和$array2。目標是建立一個新陣列 $array3,它合併兩個陣列中的所有鍵值對。 此外,提供的陣列具有唯一的 ID,而名稱可能重疊。要求是建構一...
    程式設計 發佈於2024-11-15
  • 如何使用 Twitter Bootstrap 對齊表格中的文字?
    如何使用 Twitter Bootstrap 對齊表格中的文字?
    Twitter Bootstrap 中的表格文字對齊在Twitter 的Bootstrap 架構中,您可以使用指定的文字對齊類別來對齊表格內的文本。 Bootstrap 3text-left:左對齊文字text-center:居中對齊文字text -right:右對齊文字Bootstrap 4te...
    程式設計 發佈於2024-11-15
  • 如何讓 CSS 中的空白表格儲存格的邊框可見?
    如何讓 CSS 中的空白表格儲存格的邊框可見?
    我可以在 CSS 中讓空白儲存格的邊框可見嗎? 在 Internet Explorer 7 中,預設可能不會顯示空白儲存格的邊框。不過,有幾種方法可以解決此問題。 使用不間斷空格如果可行,請插入不間斷空格 ( )進入空單元格可以強制瀏覽器渲染帶有邊框的單元格。 純 CSS解決方案對於純CSS解決方案...
    程式設計 發佈於2024-11-15
  • 如何將 Python 清單轉換為 CSV 檔案?
    如何將 Python 清單轉換為 CSV 檔案?
    將Python 清單清單匯出至CSV 檔案將Python 清單清單轉換為CSV 檔案,確保每個子清單中都會保留不同類型(浮點型、整數型、字串型)的資料。所需的 CSV 格式涉及使用逗號分隔每個子清單中的元素並垂直對齊子清單。 要實現此目的,您可以利用 Python 的內建 csv 模組:import...
    程式設計 發佈於2024-11-15
  • 測試限制:了解軟體測試的邊界
    測試限制:了解軟體測試的邊界
    软件测试是确保软件质量、稳定性和功能的开发过程的重要组成部分。然而,尽管测试很重要,但它也有其局限性。虽然它可以揭示缺陷,但它不能保证应用程序完全没有错误。了解这些限制有助于企业和开发人员设定切合实际的期望并优化他们的测试流程。在本文中,我们将探讨软件测试的主要局限性及其带来的挑战。 无法测试每个...
    程式設計 發佈於2024-11-15
  • 如何有效地將檔案載入到`std::vector`中?
    如何有效地將檔案載入到`std::vector`中?
    有效率地將檔案載入到std::vector有效率地將檔案載入std::vector ,必須避免不必要的複製和記憶體重新分配。雖然利用 Reserve 和 read() 的原始方法看起來可能很直接,但單獨的 Reserve() 並不會改變向量的容量。 使用迭代器的規範方法:規範方法使用輸入流迭代器來方...
    程式設計 發佈於2024-11-15
  • 如何在 Go 中將陣列元素直接解壓縮為變數?
    如何在 Go 中將陣列元素直接解壓縮為變數?
    在 Go 中解包數組元素Go 缺乏將數組元素直接解包到 Python 中的變數的便捷語法。雖然提問者使用中間變數的初始方法有效,但它可能會導致程式碼混亂,尤其是在複雜的場景中。 多個回傳值為了解決這個問題,建議使用解決方案是建立一個傳回多個值的函數。例如,要拆分字串並將結果解壓縮為兩個變量,可以使用...
    程式設計 發佈於2024-11-15
  • 「n:m」和「1:n」關係如何塑造資料庫設計?
    「n:m」和「1:n」關係如何塑造資料庫設計?
    理解關聯式資料庫設計:「n:m」與「1:n」的意義在資料庫設計中,符號「 n :m」和「1:n」在表示表或實體之間的關係方面起著至關重要的作用。這些符號表示它們關聯的基數。 "n:m" 關係:多對多「n:m」關係表示多對多兩個資料實體之間的對多關聯。這意味著對於一個表中的每個實體...
    程式設計 發佈於2024-11-15
  • 如何在 Java 中尋找重定向的 URL?
    如何在 Java 中尋找重定向的 URL?
    在Java 中查找重定向URL在Java 中訪問網頁時,處理URL 重定向到備用位置的情況至關重要。若要確定已重新導向的 URL,您可以使用 URL 和 URLConnection 類別。 使用 URLConnection.getUrl()使用 URLConnection 建立連線後,您可以擷取連線...
    程式設計 發佈於2024-11-15
  • 在 C++ 中將字串轉換為整數時如何處理轉換錯誤?
    在 C++ 中將字串轉換為整數時如何處理轉換錯誤?
    使用 C 中的錯誤處理將字串轉換為 int 將字串轉換為整數是程式設計中的常見任務。但是,在某些情況下,字串值可能無法成功轉換為整數。在這種情況下,優雅地處理轉換失敗至關重要。 boost::lexical_cast將字串轉換為 int 時出現錯誤的最直接方法之一處理方法是使用 boost::lex...
    程式設計 發佈於2024-11-15
  • 如何在 JavaScript 中存取 PHP 變數?
    如何在 JavaScript 中存取 PHP 變數?
    在 JavaScript 中存取 PHP 變數直接在 JavaScript 中存取 PHP 變數是一個挑戰。但是,有一些方法可以實現此目的:使用嵌入式PHP 語句:在JavaScript 區塊中嵌入PHP 程式碼允許您將PHP 變數指派給JavaScript 變數:<script type=&...
    程式設計 發佈於2024-11-15
  • 多執行緒概念 部分死鎖
    多執行緒概念 部分死鎖
    欢迎来到我们的多线程系列的第 3 部分! 在第 1 部分中,我们探讨了原子性 和 不变性。 在第 2 部分中,我们讨论了饥饿。 在这一部分中,我们将深入研究多线程中死锁的机制。原因是什么,如何识别以及可以使用的预防策略,以避免将代码变成僵局。应用程序逐渐停止,通常没有任何明显的错误,让开发人员...
    程式設計 發佈於2024-11-15

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3