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

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]删除
最新教程 更多>
  • 如何使用 Twitter Bootstrap 对齐表格中的文本?
    如何使用 Twitter Bootstrap 对齐表格中的文本?
    Twitter Bootstrap 中的表格文本对齐在 Twitter 的 Bootstrap 框架中,您可以使用指定的文本对齐类来对齐表格内的文本。 Bootstrap 3text-left:左对齐文本text-center:居中对齐文本text-right:右对齐文本Bootstrap 4tex...
    编程 发布于2024-11-15
  • 除了“if”语句之外:还有什么地方可以在不进行强制转换的情况下使用具有显式“bool”转换的类型?
    除了“if”语句之外:还有什么地方可以在不进行强制转换的情况下使用具有显式“bool”转换的类型?
    无需强制转换即可上下文转换为 bool您的类定义了对 bool 的显式转换,使您能够在条件语句中直接使用其实例“t”。然而,这种显式转换提出了一个问题:“t”在哪里可以在不进行强制转换的情况下用作 bool?上下文转换场景C 标准指定了四种值可以根据上下文转换为 bool 的主要场景:语句:if、w...
    编程 发布于2024-11-15
  • 如何使 CSS 中的空表格单元格的边框可见?
    如何使 CSS 中的空表格单元格的边框可见?
    我可以在 CSS 中使空单元格的边框可见吗?在 Internet Explorer 7 中,默认情况下可能不会显示空单元格的边框。不过,有几种方法可以解决此问题。使用不间断空格如果可行,请插入不间断空格 ( )进入空单元格可以强制浏览器渲染带有边框的单元格。纯 CSS 解决方案对于纯 CS...
    编程 发布于2024-11-15
  • 如何将 Python 列表转换为 CSV 文件?
    如何将 Python 列表转换为 CSV 文件?
    将 Python 列表列表导出到 CSV 文件您的目标是将 Python 列表列表转换为 CSV 文件,确保每个子列表中都会保留不同类型(浮点型、整数型、字符串型)的数据。所需的 CSV 格式涉及使用逗号分隔每个子列表中的元素并垂直对齐子列表。要实现此目的,您可以利用 Python 的内置 csv ...
    编程 发布于2024-11-15
  • 测试限制:了解软件测试的边界
    测试限制:了解软件测试的边界
    软件测试是确保软件质量、稳定性和功能的开发过程的重要组成部分。然而,尽管测试很重要,但它也有其局限性。虽然它可以揭示缺陷,但它不能保证应用程序完全没有错误。了解这些限制有助于企业和开发人员设定切合实际的期望并优化他们的测试流程。在本文中,我们将探讨软件测试的主要局限性及其带来的挑战。 无法测试每个...
    编程 发布于2024-11-15
  • 如何有效地将文件加载到`std::vector`中?
    如何有效地将文件加载到`std::vector`中?
    高效地将文件加载到 std::vector高效地将文件加载到 std::vector,必须避免不必要的复制和内存重新分配。虽然利用 Reserve 和 read() 的原始方法可能看起来很直接,但单独的 Reserve() 并不会改变向量的容量。使用迭代器的规范方法:规范方法使用输入流迭代器来方便地...
    编程 发布于2024-11-15
  • 如何修复 macOS 上 Django 中的“配置不正确:加载 MySQLdb 模块时出错”?
    如何修复 macOS 上 Django 中的“配置不正确:加载 MySQLdb 模块时出错”?
    MySQL配置不正确:相对路径的问题在Django中运行python manage.py runserver时,可能会遇到以下错误:ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-...
    编程 发布于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
  • 如何使用 MySQL 查找今天生日的用户?
    如何使用 MySQL 查找今天生日的用户?
    如何使用 MySQL 识别今天生日的用户使用 MySQL 确定今天是否是用户的生日涉及查找生日匹配的所有行今天的日期。这可以通过一个简单的 MySQL 查询来实现,该查询将存储为 UNIX 时间戳的生日与今天的日期进行比较。以下 SQL 查询将获取今天有生日的所有用户: FROM USERS ...
    编程 发布于2024-11-15
  • 在 C++ 中将字符串转换为整数时如何处理转换错误?
    在 C++ 中将字符串转换为整数时如何处理转换错误?
    使用 C 中的错误处理将字符串转换为 int 将字符串转换为整数是编程中的常见任务。但是,在某些情况下,字符串值可能无法成功转换为整数。在这种情况下,优雅地处理转换失败至关重要。boost::lexical_cast将字符串转换为 int 时出现错误的最直接方法之一处理方法是使用 boost::le...
    编程 发布于2024-11-15
  • 如何在 JavaScript 中访问 PHP 变量?
    如何在 JavaScript 中访问 PHP 变量?
    在 JavaScript 中访问 PHP 变量直接在 JavaScript 中访问 PHP 变量是一个挑战。但是,有一些方法可以实现此目的:使用嵌入式 PHP 语句:在 JavaScript 块中嵌入 PHP 代码允许您将 PHP 变量分配给 JavaScript 变量:<script typ...
    编程 发布于2024-11-15
  • 如何在 PHP 中组合两个关联数组,同时保留唯一 ID 并处理重复名称?
    如何在 PHP 中组合两个关联数组,同时保留唯一 ID 并处理重复名称?
    在 PHP 中组合关联数组在 PHP 中,将两个关联数组组合成一个数组是一项常见任务。考虑以下请求:问题描述:提供的代码定义了两个关联数组,$array1和$array2。目标是创建一个新数组 $array3,它合并两个数组中的所有键值对。 此外,提供的数组具有唯一的 ID,而名称可能重合。要求是构...
    编程 发布于2024-11-15
  • 多线程概念 部分死锁
    多线程概念 部分死锁
    欢迎来到我们的多线程系列的第 3 部分! 在第 1 部分中,我们探讨了原子性 和 不变性。 在第 2 部分中,我们讨论了饥饿。 在这一部分中,我们将深入研究多线程中死锁的机制。原因是什么,如何识别以及可以使用的预防策略,以避免将代码变成僵局。应用程序逐渐停止,通常没有任何明显的错误,让开发人员...
    编程 发布于2024-11-15

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

Copyright© 2022 湘ICP备2022001581号-3