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
浏览:935

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]删除
最新教程 更多>
  • 如何解决AppEngine中“无法猜测文件类型,使用application/octet-stream...”错误?
    如何解决AppEngine中“无法猜测文件类型,使用application/octet-stream...”错误?
    appEngine静态文件mime type override ,静态文件处理程序有时可以覆盖正确的mime类型,在错误消息中导致错误消息:“无法猜测mimeType for for file for file for [File]。 application/application/octet...
    编程 发布于2025-05-03
  • `console.log`显示修改后对象值异常的原因
    `console.log`显示修改后对象值异常的原因
    foo = [{id:1},{id:2},{id:3},{id:4},{id:id:5},],]; console.log('foo1',foo,foo.length); foo.splice(2,1); console.log('foo2', foo, foo....
    编程 发布于2025-05-03
  • 图片在Chrome中为何仍有边框?`border: none;`无效解决方案
    图片在Chrome中为何仍有边框?`border: none;`无效解决方案
    在chrome 在使用Chrome and IE9中的图像时遇到的一个频繁的问题是围绕图像的持续薄薄边框,尽管指定了图像,尽管指定了;和“边境:无;”在CSS中。要解决此问题,请考虑以下方法: Chrome具有忽略“ border:none; none;”的已知错误,风格。要解决此问题,请使用以下...
    编程 发布于2025-05-03
  • 如何使用Java.net.urlConnection和Multipart/form-data编码使用其他参数上传文件?
    如何使用Java.net.urlConnection和Multipart/form-data编码使用其他参数上传文件?
    使用http request 上传文件上传到http server,同时也提交其他参数,java.net.net.urlconnection and Multipart/form-data Encoding是普遍的。 Here's a breakdown of the process:Mu...
    编程 发布于2025-05-03
  • PHP阵列键值异常:了解07和08的好奇情况
    PHP阵列键值异常:了解07和08的好奇情况
    PHP数组键值问题,使用07&08 在给定数月的数组中,键值07和08呈现令人困惑的行为时,就会出现一个不寻常的问题。运行print_r($月份)返回意外结果:键“ 07”丢失,而键“ 08”分配给了9月的值。此问题源于PHP对领先零的解释。当一个数字带有0(例如07或08)的前缀时,PHP将...
    编程 发布于2025-05-03
  • 在PHP中如何高效检测空数组?
    在PHP中如何高效检测空数组?
    在PHP 中检查一个空数组可以通过各种方法在PHP中确定一个空数组。如果需要验证任何数组元素的存在,则PHP的松散键入允许对数组本身进行直接评估:一种更严格的方法涉及使用count()函数: if(count(count($ playerList)=== 0){ //列表为空。 } 对...
    编程 发布于2025-05-03
  • 如何有效地转换PHP中的时区?
    如何有效地转换PHP中的时区?
    在PHP 利用dateTime对象和functions DateTime对象及其相应的功能别名为时区转换提供方便的方法。例如: //定义用户的时区 date_default_timezone_set('欧洲/伦敦'); //创建DateTime对象 $ dateTime = ne...
    编程 发布于2025-05-03
  • 如何避免Go语言切片时的内存泄漏?
    如何避免Go语言切片时的内存泄漏?
    ,a [j:] ...虽然通常有效,但如果使用指针,可能会导致内存泄漏。这是因为原始的备份阵列保持完整,这意味着新切片外部指针引用的任何对象仍然可能占据内存。 copy(a [i:] 对于k,n:= len(a)-j i,len(a); k
    编程 发布于2025-05-03
  • 在JavaScript中如何并发运行异步操作并正确处理错误?
    在JavaScript中如何并发运行异步操作并正确处理错误?
    同意操作execution 在执行asynchronous操作时,相关的代码段落会遇到一个问题,当执行asynchronous操作:此实现在启动下一个操作之前依次等待每个操作的完成。要启用并发执行,需要进行修改的方法。 第一个解决方案试图通过获得每个操作的承诺来解决此问题,然后单独等待它们: co...
    编程 发布于2025-05-03
  • 如何有效地选择熊猫数据框中的列?
    如何有效地选择熊猫数据框中的列?
    在处理数据操作任务时,在Pandas DataFrames 中选择列时,选择特定列的必要条件是必要的。在Pandas中,选择列的各种选项。选项1:使用列名 如果已知列索引,请使用ILOC函数选择它们。请注意,python索引基于零。 df1 = df.iloc [:,0:2]#使用索引0和1 c...
    编程 发布于2025-05-03
  • 在JavaScript中如何获取实际渲染的字体,当CSS字体属性未定义时?
    在JavaScript中如何获取实际渲染的字体,当CSS字体属性未定义时?
    Accessing Actual Rendered Font when Undefined in CSSWhen accessing the font properties of an element, the JavaScript object.style.fontFamily and objec...
    编程 发布于2025-05-03
  • 反射动态实现Go接口用于RPC方法探索
    反射动态实现Go接口用于RPC方法探索
    在GO 使用反射来实现定义RPC式方法的界面。例如,考虑一个接口,例如:键入myService接口{ 登录(用户名,密码字符串)(sessionId int,错误错误) helloworld(sessionid int)(hi String,错误错误) } 替代方案而不是依靠反射...
    编程 发布于2025-05-03
  • Java中假唤醒真的会发生吗?
    Java中假唤醒真的会发生吗?
    在Java中的浪费唤醒:真实性或神话?在Java同步中伪装唤醒的概念已经是讨论的主题。尽管存在这种行为的潜力,但问题仍然存在:它们实际上是在实践中发生的吗? Linux的唤醒机制根据Wikipedia关于伪造唤醒的文章,linux实现了pthread_cond_wait()功能的Linux实现,利用...
    编程 发布于2025-05-03
  • 如何修复\“常规错误:2006 MySQL Server在插入数据时已经消失\”?
    如何修复\“常规错误:2006 MySQL Server在插入数据时已经消失\”?
    How to Resolve "General error: 2006 MySQL server has gone away" While Inserting RecordsIntroduction:Inserting data into a MySQL database can...
    编程 发布于2025-05-03
  • 在UTF8 MySQL表中正确将Latin1字符转换为UTF8的方法
    在UTF8 MySQL表中正确将Latin1字符转换为UTF8的方法
    在UTF8表中将latin1字符转换为utf8 ,您遇到了一个问题,其中含义的字符(例如,“jáuòiñe”)在utf8 table tabled tablesset中被extect(例如,“致电。为了解决此问题,您正在尝试使用“ mb_convert_encoding”和“ iconv”转换受...
    编程 发布于2025-05-03

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3