├── .gitignore ├── client ├── public │ ├── robots.txt │ ├── favicon.ico │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── index.html ├── src │ ├── setupTests.js │ ├── App.test.js │ ├── index.css │ ├── reportWebVitals.js │ ├── index.js │ ├── App.css │ ├── logo.svg │ └── App.js ├── .gitignore ├── package.json └── README.md ├── config └── default.json ├── server ├── elasticsearch │ └── client.js ├── create-api-key.js ├── server.js └── data_management │ └── retrieve_and_ingest_data.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /client/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /client/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LisaHJung/Beginners-guide-to-creating-a-full-stack-JavaScript-app-with-Elasticsearch/HEAD/client/public/favicon.ico -------------------------------------------------------------------------------- /client/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LisaHJung/Beginners-guide-to-creating-a-full-stack-JavaScript-app-with-Elasticsearch/HEAD/client/public/logo192.png -------------------------------------------------------------------------------- /client/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LisaHJung/Beginners-guide-to-creating-a-full-stack-JavaScript-app-with-Elasticsearch/HEAD/client/public/logo512.png -------------------------------------------------------------------------------- /client/src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /config/default.json: -------------------------------------------------------------------------------- 1 | { 2 | "elastic": { 3 | "cloudID": "paste your deployment name:cloud id details here", 4 | "username": "elastic", 5 | "password": "paste your password here", 6 | "apiKey": "paste your API key here" 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /client/src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /client/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /client/src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /server/elasticsearch/client.js: -------------------------------------------------------------------------------- 1 | const { Client } = require('@elastic/elasticsearch'); 2 | const config = require('config'); 3 | 4 | const elasticConfig = config.get('elastic'); 5 | 6 | const client = new Client({ 7 | cloud: { 8 | id: elasticConfig.cloudID, 9 | }, 10 | auth: { 11 | apiKey: elasticConfig.apiKey 12 | }, 13 | }); 14 | 15 | client.ping() 16 | .then(response => console.log("You are connected to Elasticsearch!")) 17 | .catch(error => console.error("Elasticsearch is not connected.")) 18 | 19 | module.exports = client; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "season_2_repo", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "nodemon server/server.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "@elastic/elasticsearch": "8.2.1", 15 | "axios": "0.27.2", 16 | "config": "3.3.7", 17 | "cors": "2.8.5", 18 | "express": "4.18.1", 19 | "log-timestamp": "0.3.0", 20 | "nodemon": "2.0.19" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /client/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | const root = ReactDOM.createRoot(document.getElementById('root')); 8 | root.render( 9 | 10 | 11 | 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /client/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /server/create-api-key.js: -------------------------------------------------------------------------------- 1 | const client = require('./elasticsearch/client'); 2 | 3 | async function generateApiKeys(opts) { 4 | const body = await client.security.createApiKey({ 5 | body: { 6 | name: 'earthquake_app', 7 | role_descriptors: { 8 | earthquakes_example_writer: { 9 | cluster: ['monitor'], 10 | index: [ 11 | { 12 | names: ['earthquakes'], 13 | privileges: ['create_index', 'write', 'read', 'manage'], 14 | }, 15 | ], 16 | }, 17 | }, 18 | }, 19 | }); 20 | return Buffer.from(`${body.id}:${body.api_key}`).toString('base64'); 21 | } 22 | 23 | generateApiKeys() 24 | .then(console.log) 25 | .catch((err) => { 26 | console.error(err); 27 | process.exit(1); 28 | }); -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "5.16.5", 7 | "@testing-library/react": "13.3.0", 8 | "@testing-library/user-event": "13.5.0", 9 | "axios": "0.27.2", 10 | "react": "18.2.0", 11 | "react-dom": "18.2.0", 12 | "react-scripts": "5.0.1", 13 | "web-vitals": "2.1.4" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "proxy": "http://localhost:3001", 22 | "eslintConfig": { 23 | "extends": [ 24 | "react-app", 25 | "react-app/jest" 26 | ] 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | const { Client } = require('@elastic/elasticsearch'); 2 | const client = require('./elasticsearch/client'); 3 | const express = require('express'); 4 | const cors = require('cors'); 5 | 6 | const app = express(); 7 | 8 | const data = require('./data_management/retrieve_and_ingest_data'); 9 | 10 | app.use('/ingest_data', data); 11 | 12 | app.use(cors()); 13 | 14 | app.get('/results', (req, res) => { 15 | const passedType = req.query.type; 16 | const passedMag = req.query.mag; 17 | const passedLocation = req.query.location; 18 | const passedDateRange = req.query.dateRange; 19 | const passedSortOption = req.query.sortOption; 20 | 21 | async function sendESRequest() { 22 | const body = await client.search({ 23 | index: 'earthquakes', 24 | body: { 25 | sort: [ 26 | { 27 | mag: { 28 | order: passedSortOption, 29 | }, 30 | }, 31 | ], 32 | size: 300, 33 | query: { 34 | bool: { 35 | filter: [ 36 | { 37 | term: { type: passedType }, 38 | }, 39 | { 40 | range: { 41 | mag: { 42 | gte: passedMag, 43 | }, 44 | }, 45 | }, 46 | { 47 | match: { place: passedLocation }, 48 | }, 49 | // for those who use prettier, make sure there is no whitespace. 50 | { 51 | range: { 52 | '@timestamp': { 53 | gte: `now-${passedDateRange}d/d`, 54 | lt: 'now/d', 55 | }, 56 | }, 57 | }, 58 | ], 59 | }, 60 | }, 61 | }, 62 | }); 63 | res.json(body.hits.hits); 64 | } 65 | sendESRequest(); 66 | }); 67 | 68 | const PORT = process.env.PORT || 3001; 69 | 70 | app.listen(PORT, () => console.group(`Server started on ${PORT}`)); -------------------------------------------------------------------------------- /client/src/App.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300&display=swap'); 2 | 3 | * { 4 | font-family: 'Open Sans Condensed', sans-serif; 5 | } 6 | 7 | body { 8 | background-image: url(https://i.imgur.com/lx59qEN.jpg); 9 | background-position-x: center; 10 | background-position-y: center; 11 | background-size: 100% auto; 12 | background-repeat: no-repeat; 13 | background-attachment: fixed; 14 | background-origin: initial; 15 | background-clip: initial; 16 | margin: 0; 17 | } 18 | 19 | div { 20 | color: white; 21 | } 22 | 23 | .nav { 24 | width: 100%; 25 | float: left; 26 | } 27 | 28 | .nav-bar { 29 | margin: 0; 30 | padding: 8px; 31 | height: 50px; 32 | background-color: rgb(28, 28, 28); 33 | } 34 | 35 | .nav-bar li { 36 | display: inline; 37 | margin-top: 0px; 38 | font-size: 2.5em; 39 | } 40 | 41 | .directions { 42 | margin-left: 14px; 43 | font-size: 1.5em; 44 | } 45 | 46 | .main { 47 | display: flex; 48 | flex-direction: column; 49 | font-family: inherit; 50 | } 51 | .type-selector { 52 | margin-left: -30px; 53 | } 54 | 55 | .type-selector li { 56 | float: left; 57 | display: inline; 58 | margin-left: 4px; 59 | margin-top: -19px; 60 | font-style: inherit; 61 | } 62 | 63 | .type-selector li:focus, 64 | .type-selector li:hover { 65 | outline: none; 66 | border: 1px solid #bbbbbb; 67 | } 68 | 69 | .form { 70 | width: 200px; 71 | height: 16px; 72 | } 73 | 74 | .form::placeholder { 75 | color: black; 76 | opacity: 1; 77 | } 78 | 79 | .type-selector button:hover { 80 | color: #be3400; 81 | border: 0.1rem #404040 solid; 82 | } 83 | 84 | .type-selector button { 85 | height: 23px; 86 | } 87 | 88 | .search-results { 89 | display: block; 90 | width: 100%; 91 | margin-left: 13px; 92 | font-family: inherit; 93 | } 94 | 95 | .results-card { 96 | background-color: rgb(25, 129, 67, 0.7); 97 | flex: 0 1 29rem; 98 | height: 23rem; 99 | border-radius: 10px; 100 | font-weight: bold; 101 | border: 1px; 102 | float: left; 103 | margin: 1px; 104 | margin-top: 5px; 105 | margin-right: 10px; 106 | padding: 10px epx; 107 | justify-content: space-between; 108 | max-width: 400px; 109 | } 110 | 111 | .results-text { 112 | margin-left: 2rem; 113 | margin-right: 2rem; 114 | } -------------------------------------------------------------------------------- /client/src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /server/data_management/retrieve_and_ingest_data.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const router = express.Router(); 3 | const axios = require('axios'); 4 | const client = require('../elasticsearch/client'); 5 | require('log-timestamp'); 6 | 7 | const URL = `https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson`; 8 | 9 | router.get('/earthquakes', async function (req, res) { 10 | console.log('Loading Application...'); 11 | res.json('Running Application...'); 12 | 13 | indexData = async () => { 14 | try { 15 | console.log('Retrieving data from the USGS API'); 16 | 17 | const EARTHQUAKES = await axios.get(`${URL}`, { 18 | headers: { 19 | 'Content-Type': ['application/json', 'charset=utf-8'], 20 | }, 21 | }); 22 | 23 | console.log('Data retrieved!'); 24 | 25 | results = EARTHQUAKES.data.features; 26 | 27 | console.log('Indexing data...'); 28 | 29 | results.map( 30 | async (results) => ( 31 | (earthquakeObject = { 32 | place: results.properties.place, 33 | time: results.properties.time, 34 | tz: results.properties.tz, 35 | url: results.properties.url, 36 | detail: results.properties.detail, 37 | felt: results.properties.felt, 38 | cdi: results.properties.cdi, 39 | alert: results.properties.alert, 40 | status: results.properties.status, 41 | tsunami: results.properties.tsunami, 42 | sig: results.properties.sig, 43 | net: results.properties.net, 44 | code: results.properties.code, 45 | sources: results.properties.sources, 46 | nst: results.properties.nst, 47 | dmin: results.properties.dmin, 48 | rms: results.properties.rms, 49 | mag: results.properties.mag, 50 | magType: results.properties.magType, 51 | type: results.properties.type, 52 | longitude: results.geometry.coordinates[0], 53 | latitude: results.geometry.coordinates[1], 54 | depth: results.geometry.coordinates[2], 55 | }), 56 | await client.index({ 57 | index: 'earthquakes', 58 | id: results.id, 59 | body: earthquakeObject, 60 | pipeline: 'earthquake_data_pipeline', 61 | }) 62 | ) 63 | ); 64 | 65 | if (EARTHQUAKES.data.length) { 66 | indexData(); 67 | } else { 68 | console.log('Data has been indexed successfully!'); 69 | } 70 | } catch (err) { 71 | console.log(err); 72 | } 73 | 74 | console.log('Preparing for the next round of indexing...'); 75 | }; 76 | indexData(); 77 | }); 78 | 79 | module.exports = router; 80 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser. 13 | 14 | The page will reload when you make changes.\ 15 | You may also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 35 | 36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. 39 | 40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Beginner's guide to building a full stack JavaScript web app with Elasticsearch 2 | 3 | Welcome to season 2 of Mini Beginner's Crash Course to Elasticsearch and Kibana. 4 | 5 | In this series, we will be building a full stack app(Node.js & React) that enables users to search for earthquake data stored in Elasticsearch! 6 | 7 | ![image](https://media.giphy.com/media/MVcpZ83Nwb2iICeUTD/giphy.gif) 8 | 9 | ## Two Ways to Learn 10 | We all have preferred method of learning so choose the format that works for you: 11 | 12 | **1. [Video format](https://ela.st/mini-beginners-crash-course)(YouTube playlist)** 13 | 14 | Season 2 video titles start with S2 and has a thumbnail background similar to the following! 15 | image 16 | 17 | **2. [Blog format](https://dev.to/lisahjung/beginners-guide-to-building-a-full-stack-app-nodejs-react-with-elasticsearch-5347) (Dev.to)** 18 | 19 | ## How to use this repo 20 | This repo contains multiple branches that serve as complementary resource to season 2 YouTube episode/blog. 21 | 22 | - [main (final product)](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-Javascript-app-with-Elasticsearch/tree/main) 23 | - [1-build-a-server](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-Javascript-app-with-Elasticsearch/tree/1-build-a-server) 24 | - [2-connect-server-to-Elastic-Cloud-via-basic-authentication](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-Javascript-app-with-Elasticsearch/tree/2-connect-server-to-Elastic-Cloud-via-basic-authentication) 25 | - [3-connect-server-to-Elastic-Cloud-via-apiKey](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-Javascript-app-with-Elasticsearch/tree/3-connect-server-to-Elastic-Cloud-via-apiKey) 26 | - [4-retrieve_and_ingest_data](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-Javascript-app-with-Elasticsearch/tree/4-retrieve_and_ingest_data) 27 | - [5-build_the_client](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-JavaScript-app-with-Elasticsearch/tree/5-build_the_client) 28 | - [6-manage_elasticsearch_request](https://github.com/LisaHJung/beginners-guide-to-creating-a-full-stack-Javascript-app-with-Elasticsearch/tree/6-manage_elasticsearch_request) 29 | 30 | :sparkles:**Follow the YouTube episodes/blogs in sequential order and use this repo as a supplementary resource. The videos/blogs contain the link to the corresponding repo branch.**:sparkles: 31 | 32 | ## Running the App locally 33 | If you wish to download the project, follow these instructions. 34 | 35 | ### Downloading the repo 36 | Go to the page of the branch you wish to download. 37 | 38 | Click on the `Code` button(blue box) to display the drop down menu. 39 | 40 | image 41 | 42 | Click on the `Download Zip` option(red box). 43 | 44 | Once the code is downloaded, double click on the file to unzip it. 45 | 46 | Move the unzipped file to your desired location. 47 | 48 | I recommend that you change the name to something shorter as the downloaded project will have the following name: 49 | 50 | *beginners-guide-to-creating-a-full-stack-JavaScript-app-with-Elasticsearch-name-of-the-branch.zip* 51 | 52 | Cd into the project directory. 53 | 54 | :sparkles:**Depending on which episode/blog/branch you are working on, the corresponding repo will contain only the server side code or both the server and client side code.**:sparkles: 55 | 56 | ### Start the server 57 | 58 | Execute these commands in the terminal in the following order. 59 | ```javascript 60 | //in the project directory 61 | npm install 62 | npm start 63 | ``` 64 | 65 | ### Start the client 66 | 67 | Execute these commands *in a new terminal* in the following order. 68 | ```javascript 69 | //in the project directory 70 | cd client 71 | npm install 72 | npm start 73 | ``` 74 | :sparkles:**The recommended browser for this project is Google Chrome.**:sparkles: 75 | 76 | ### Don't forget! 77 | This project requires creating an Elastic Cloud deployment and adding the Elastic Cloud access credentials to the `config/default.json` file. 78 | 79 | The steps on how to accomplish these tasks are outlined in the following blogs: 80 | - [Part 3: Create an Elastic Cloud deployment](https://dev.to/lisahjung/part-3-create-an-elastic-cloud-deployment-36bn) 81 | - [Part 4: Securely connect Node.js server to Elastic Cloud](https://dev.to/lisahjung/part-4-securely-connect-nodejs-server-to-elastic-cloud-4f22) 82 | 83 | :sparkles:**When running the project from branches 2-6, be sure to update the `config/default.json` file with your access credentials before running the project!**:sparkles: 84 | -------------------------------------------------------------------------------- /client/src/App.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | import { useState } from 'react'; 3 | import './App.css'; 4 | 5 | const App = () => { 6 | const [chosenType, setChosenType] = useState(null); 7 | const [chosenMag, setChosenMag] = useState(null); 8 | const [chosenLocation, setChosenLocation] = useState(null); 9 | const [chosenDateRange, setChosenDateRange] = useState(null); 10 | const [chosenSortOption, setchosenSortOption] = useState(null); 11 | const [documents, setDocuments] = useState(null); 12 | 13 | const sendSearchRequest = () => { 14 | const results = { 15 | method: 'GET', 16 | url: 'http://localhost:3001/results', 17 | params: { 18 | type: chosenType, 19 | mag: chosenMag, 20 | location: chosenLocation, 21 | dateRange: chosenDateRange, 22 | sortOption: chosenSortOption, 23 | }, 24 | }; 25 | axios 26 | .request(results) 27 | .then((response) => { 28 | console.log(response.data); 29 | setDocuments(response.data); 30 | }) 31 | .catch((error) => { 32 | console.error(error); 33 | }); 34 | }; 35 | 36 | return ( 37 |
38 | 43 |

44 | {' '} 45 | Search for earthquakes using the following criteria: 46 |

47 |
48 |
49 |
    50 |
  • 51 | 63 |
  • 64 |
  • 65 | 78 |
  • 79 |
  • 80 |
    81 | 90 |
    91 |
  • 92 |
  • 93 | 105 |
  • 106 |
  • 107 | 117 |
  • 118 |
  • 119 | 120 |
  • 121 |
122 |
123 | {documents && ( 124 |
125 | {documents.length > 0 ? ( 126 |

Number of hits: {documents.length}

127 | ) : ( 128 |

No results found. Try broadening your search criteria.

129 | )} 130 | {documents.map((document) => ( 131 |
132 |
133 |

Type: {document._source.type}

134 |

Time: {document._source['@timestamp']}

135 |

Location: {document._source.place}

136 |

Latitude: {document._source.coordinates.lat}

137 |

Longitude: {document._source.coordinates.lon}

138 |

Magnitude: {document._source.mag}

139 |

Depth: {document._source.depth}

140 |

Significance: {document._source.sig}

141 |

Event URL: {document._source.url}

142 |
143 |
144 | ))} 145 |
146 | )} 147 |
148 |
149 | ); 150 | }; 151 | 152 | export default App; --------------------------------------------------------------------------------