Vue Storefront: Raising the backend

I continue to promote my referral link to IaaS provider Exostate and file the second article of my advertising campaign. In the first article, I deployed and launched the ' vue-storefront ' application as a separate server, without being tied to any data, masking in such an absurd way my commercial interest in selling Exoscale services. My pathetic attempts were successfully opened by aol-nnov colleague and I was taken to clean water. Well, the second stage of my advertising campaign is no different from the first one - the same page with an error:







image







The front is now tied to the backend (' vue-storefront-api '), but in the client’s browser the same bleak message β€œ Something went wrong ... ”. Therefore, having discarded the false modesty, I rendered my referral link to the kat, and placed the entire disguise under the kat - details of how I combined the "Vue Storefront" with the "Vue Storefront API".







purpose



At this stage, I set myself the task of raising a minimally functional backend and closing the front on it.







Schematic



To make it a little clearer, I bring the picture that I developed according to the analysis of application configs:







image







The vue-storefront



deployment vue-storefront



I cited in a previous article . Thus, vue-storefront-api



, Elasticsearch and Redis need to be raised. Judging by the address / port in the configQL, vue-storefront-api



is responsible for GraphQL itself.







Work environment



For this stage, I also had a small version of the Linux Ubuntu 18.04 LTS 64-bit server (2x 2198 MHz CPU, 2 GB RAM, 10 GB disk). What's nice about IaaS is the ability to start over and over again from scratch, throwing previous failed experiments into the trash.







First stage



Here is the script for repeating the steps of the first step:







 #!/usr/bin/env/bash set -e sudo apt update sleep 2 # wait 2 second before upgrade to prevent en error sudo apt upgrade -y sudo curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - sudo apt install -y nodejs sudo curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - sudo echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt update && sudo apt install yarn sudo npm install pm2@latest -g sudo npm install @vue-storefront/cli@latest -g # interactive command below: use 'Stable versions' / 'v1.10.4' (or latest) / 'Manual installation' vsf init
      
      





It will be necessary to look towards the automatic deployment of vue-storefront



, without human intervention. But for now, with vsf



.







Install Elasticsearch



 # apt install openjdk-11-jre-headless -y # java -version openjdk version "11.0.4" 2019-07-16 OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3) OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing) # apt-get install apt-transport-https # wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - # add-apt-repository "deb https://artifacts.elastic.co/packages/7.x/apt stable main" # apt update # apt install elasticsearch -y
      
      





Before starting the service, you need to change the configuration of Elasticsearch so that you can reach the service from the outside (this is not necessary, but it’s more convenient for me):







 $ sudo nano /etc/elasticsearch/elasticsearch.yml cluster.name: habr_demo node.name: exo01 path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch network.host: 0.0.0.0 discovery.seed_hosts: []
      
      





Service Launch:







 # service elasticsearch restart
      
      





And checking its performance:







 $ curl -X GET "http://localhost:9200/?pretty" { "name" : "exo01", "cluster_name" : "habr_demo", "cluster_uuid" : "_na_", "version" : { "number" : "7.4.2", "build_flavor" : "default", "build_type" : "deb", "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96", "build_date" : "2019-10-28T20:40:44.881551Z", "build_snapshot" : false, "lucene_version" : "8.2.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
      
      





Install Redis



 # apt install redis-server -y
      
      





Just like Elasticsearch, I configure Redis to be accessible from the outside:







 # nano /etc/redis/redis.conf bind 0.0.0.0 # service redis-server start
      
      





I start the service and check its performance:







 # service redis-server start # redis-cli > set test "It's working!" > get test > exit
      
      





Install vue-storefront-api





 $ cd ~ $ git clone https://github.com/DivanteLtd/vue-storefront-api.git
      
      





Linking the front to the backend



Front configuration



Available front configuration options can be found in ~/vue-storefront/config/default.json



. I overridden some parameters in ~/vue-storefront/config/local.json



:







 { "server": { "host": "0.0.0.0", "port": 3000 }, "redis": { "host": "194.182.181.149", "port": 6379, "db": 0 }, "graphql": { "host": "194.182.181.149", "port": 8080 }, "api": { "url": "http://194.182.181.149:8080" }, "elasticsearch": {} }
      
      





server.host|.port



- I bind a nodejs server that will output content for the front to all available ip addresses on the corresponding port.







redis



and graphql



- I bind to the external ip-address of my test server. I don’t know whether the nodejs server issuing the content uses these settings, or whether these settings are used on the client side in the browser, so I put the external address.







api.uri



- here you definitely need to register the external IP address of the server, because at the previous stage, I saw API calls from the browser (PWA applications) in the request logs ( Network



tab in the toolbar). This is the port address on which the nodejs server from the vue-storefront-api



application will hang.







Backend configuration



Available configuration options can also be viewed in ~/vue-storefront-api/config/default.json



. Here are the overridden options in ~/vue-storefront-api/config/local.json



:







 { "server": { "host": "0.0.0.0", "port": 8080 }, "elasticsearch": { "host": "localhost", "port": 9200 }, "redis": { "host": "localhost", "port": 6379 } }
      
      





server.host|.port



- I bind a nodejs server that will provide content for the API to all available ip addresses on the corresponding port.







elasticsearch



and redis



are located on the same host as the API server, so I just repeat the default parameters.







Application assembly



Front



 $ cd ~/vue-storefront $ yarn install $ yarn build
      
      





Backend



 $ cd ~/vue-storefront-api $ yarn install $ yarn build
      
      





Starting and stopping applications



 $ cd ~/vue-storefront-api $ yarn start ... $ cd ~/vue-storefront $ yarn start ... [PM2][WARN] Applications server not running, starting... [PM2] App [server] launched (4 instances) β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ App name β”‚ id β”‚ mode β”‚ pid β”‚ status β”‚ restart β”‚ uptime β”‚ cpu β”‚ mem β”‚ user β”‚ watching β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ api β”‚ 0 β”‚ fork β”‚ 3690 β”‚ online β”‚ 0 β”‚ 11s β”‚ 0% β”‚ 85.2 MB β”‚ ubuntu β”‚ disabled β”‚ β”‚ o2m β”‚ 1 β”‚ fork β”‚ 3696 β”‚ online β”‚ 0 β”‚ 11s β”‚ 0% β”‚ 48.4 MB β”‚ ubuntu β”‚ disabled β”‚ β”‚ server β”‚ 2 β”‚ cluster β”‚ 3763 β”‚ online β”‚ 0 β”‚ 0s β”‚ 26.7% β”‚ 66.9 MB β”‚ ubuntu β”‚ disabled β”‚ β”‚ server β”‚ 3 β”‚ cluster β”‚ 3770 β”‚ online β”‚ 0 β”‚ 0s β”‚ 0% β”‚ 68.1 MB β”‚ ubuntu β”‚ disabled β”‚ β”‚ server β”‚ 4 β”‚ cluster β”‚ 3785 β”‚ online β”‚ 0 β”‚ 0s β”‚ 0% β”‚ 40.9 MB β”‚ ubuntu β”‚ disabled β”‚ β”‚ server β”‚ 5 β”‚ cluster β”‚ 3796 β”‚ online β”‚ 0 β”‚ 0s β”‚ 0% β”‚ 40.9 MB β”‚ ubuntu β”‚ disabled β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Use `pm2 show <id|name>` to get more details about an app Done in 1.49s.
      
      





Stop:







 $ pm2 stop all
      
      





View Logs:







 $ pm2 log
      
      





Application Connection



Application address: http : //194.182.181.149haps000/ As a result, we have an error message in the browser at the very beginning, but in the logs of the API server we can record browser calls to the API:







 $ pm2 log 3|server | 2019-11-16 07:44:33: Error during render : / 3|server | 2019-11-16 07:44:33: Error: FetchError in request to ES: FetchError: invalid json response body at http://194.182.181.149:8080/api/catalog/vue_storefront_catalog/product/_search?_source_exclude=%2A.msrp_display_actual_price_type%2Crequired_options%2Cupdated_at%2Ccreated_at%2Cattribute_set_id%2Coptions_container%2Cmsrp_display_actual_price_type%2Chas_options%2Cstock.manage_stock%2Cstock.use_config_min_qty%2Cstock.use_config_notify_stock_qty%2Cstock.stock_id%2Cstock.use_config_backorders%2Cstock.use_config_enable_qty_inc%2Cstock.enable_qty_increments%2Cstock.use_config_manage_stock%2Cstock.use_config_min_sale_qty%2Cstock.notify_stock_qty%2Cstock.use_config_max_sale_qty%2Cstock.use_config_max_sale_qty%2Cstock.qty_increments%2Csmall_image%2Csgn%2C%2A.sgn&from=0&request=%7B%22query%22%3A%7B%22bool%22%3A%7B%22filter%22%3A%7B%22bool%22%3A%7B%22must%22%3A%5B%7B%22terms%22%3A%7B%22category.name.keyword%22%3A%5B%22Tees%22%5D%7D%7D%2C%7B%22terms%22%3A%7B%22visibility%22%3A%5B2%2C3%2C4%5D%7D%7D%2C%7B%22terms%22%3A%7B%22status%22%3A%5B0%2C1%5D%7D%7D%5D%7D%7D%7D%7D%7D&size=8&sort=created_at%3Adesc reason: Unexpected end of JSON input 3|server | at w (core/server-entry.ts:23:25) 3|server | at server-bundle.js:1:468554 3|server | at processTicksAndRejections (internal/process/task_queues.js:93:5) 3|server | 2019-11-16 07:44:33: whole request [/error]: 56ms 0|api | 2019-11-16 07:44:34: OPTIONS /api/cart/create?token= 204 1.320 ms - 0 0|api | 2019-11-16 07:44:34: POST /api/cart/create?token= 200 465.416 ms - 56 0|api | 2019-11-16 07:44:34: OPTIONS /api/cart/pull?token=&cartId=35bea3b7dcf8fb841187d69489fe8c51 204 0.239 ms - 0 0|api | 2019-11-16 07:44:34: GET /api/cart/pull?token=&cartId=35bea3b7dcf8fb841187d69489fe8c51 200 103.949 ms - 24
      
      



2Cattribute_set_id% 2Coptions_container% 2Cmsrp_display_actual_price_type% 2Chas_options% 2Cstock.manage_stock% 2Cstock.use_config_min_qty% 2Cstock.use_config_notify_stock_qty% 2Cstock.stock_id% 2Cstock.use_config_backorders% 2Cstock.use_config_enable_qty_inc% 2Cstock.enable_qty_increments% 2Cstock.use_config_manage_stock% 2Cstock $ pm2 log 3|server | 2019-11-16 07:44:33: Error during render : / 3|server | 2019-11-16 07:44:33: Error: FetchError in request to ES: FetchError: invalid json response body at http://194.182.181.149:8080/api/catalog/vue_storefront_catalog/product/_search?_source_exclude=%2A.msrp_display_actual_price_type%2Crequired_options%2Cupdated_at%2Ccreated_at%2Cattribute_set_id%2Coptions_container%2Cmsrp_display_actual_price_type%2Chas_options%2Cstock.manage_stock%2Cstock.use_config_min_qty%2Cstock.use_config_notify_stock_qty%2Cstock.stock_id%2Cstock.use_config_backorders%2Cstock.use_config_enable_qty_inc%2Cstock.enable_qty_increments%2Cstock.use_config_manage_stock%2Cstock.use_config_min_sale_qty%2Cstock.notify_stock_qty%2Cstock.use_config_max_sale_qty%2Cstock.use_config_max_sale_qty%2Cstock.qty_increments%2Csmall_image%2Csgn%2C%2A.sgn&from=0&request=%7B%22query%22%3A%7B%22bool%22%3A%7B%22filter%22%3A%7B%22bool%22%3A%7B%22must%22%3A%5B%7B%22terms%22%3A%7B%22category.name.keyword%22%3A%5B%22Tees%22%5D%7D%7D%2C%7B%22terms%22%3A%7B%22visibility%22%3A%5B2%2C3%2C4%5D%7D%7D%2C%7B%22terms%22%3A%7B%22status%22%3A%5B0%2C1%5D%7D%7D%5D%7D%7D%7D%7D%7D&size=8&sort=created_at%3Adesc reason: Unexpected end of JSON input 3|server | at w (core/server-entry.ts:23:25) 3|server | at server-bundle.js:1:468554 3|server | at processTicksAndRejections (internal/process/task_queues.js:93:5) 3|server | 2019-11-16 07:44:33: whole request [/error]: 56ms 0|api | 2019-11-16 07:44:34: OPTIONS /api/cart/create?token= 204 1.320 ms - 0 0|api | 2019-11-16 07:44:34: POST /api/cart/create?token= 200 465.416 ms - 56 0|api | 2019-11-16 07:44:34: OPTIONS /api/cart/pull?token=&cartId=35bea3b7dcf8fb841187d69489fe8c51 204 0.239 ms - 0 0|api | 2019-11-16 07:44:34: GET /api/cart/pull?token=&cartId=35bea3b7dcf8fb841187d69489fe8c51 200 103.949 ms - 24



2Cstock.qty_increments% 2Csmall_image% 2Csgn% 2C% 2A.sgn & from = $ pm2 log 3|server | 2019-11-16 07:44:33: Error during render : / 3|server | 2019-11-16 07:44:33: Error: FetchError in request to ES: FetchError: invalid json response body at http://194.182.181.149:8080/api/catalog/vue_storefront_catalog/product/_search?_source_exclude=%2A.msrp_display_actual_price_type%2Crequired_options%2Cupdated_at%2Ccreated_at%2Cattribute_set_id%2Coptions_container%2Cmsrp_display_actual_price_type%2Chas_options%2Cstock.manage_stock%2Cstock.use_config_min_qty%2Cstock.use_config_notify_stock_qty%2Cstock.stock_id%2Cstock.use_config_backorders%2Cstock.use_config_enable_qty_inc%2Cstock.enable_qty_increments%2Cstock.use_config_manage_stock%2Cstock.use_config_min_sale_qty%2Cstock.notify_stock_qty%2Cstock.use_config_max_sale_qty%2Cstock.use_config_max_sale_qty%2Cstock.qty_increments%2Csmall_image%2Csgn%2C%2A.sgn&from=0&request=%7B%22query%22%3A%7B%22bool%22%3A%7B%22filter%22%3A%7B%22bool%22%3A%7B%22must%22%3A%5B%7B%22terms%22%3A%7B%22category.name.keyword%22%3A%5B%22Tees%22%5D%7D%7D%2C%7B%22terms%22%3A%7B%22visibility%22%3A%5B2%2C3%2C4%5D%7D%7D%2C%7B%22terms%22%3A%7B%22status%22%3A%5B0%2C1%5D%7D%7D%5D%7D%7D%7D%7D%7D&size=8&sort=created_at%3Adesc reason: Unexpected end of JSON input 3|server | at w (core/server-entry.ts:23:25) 3|server | at server-bundle.js:1:468554 3|server | at processTicksAndRejections (internal/process/task_queues.js:93:5) 3|server | 2019-11-16 07:44:33: whole request [/error]: 56ms 0|api | 2019-11-16 07:44:34: OPTIONS /api/cart/create?token= 204 1.320 ms - 0 0|api | 2019-11-16 07:44:34: POST /api/cart/create?token= 200 465.416 ms - 56 0|api | 2019-11-16 07:44:34: OPTIONS /api/cart/pull?token=&cartId=35bea3b7dcf8fb841187d69489fe8c51 204 0.239 ms - 0 0|api | 2019-11-16 07:44:34: GET /api/cart/pull?token=&cartId=35bea3b7dcf8fb841187d69489fe8c51 200 103.949 ms - 24





Conclusion



Managed to go one step further - Vue Storefront PWA communicates with the Vue Storefront API. Next, you need to deal with the content of the backend. What data is indexed in Elasticsearch, how it gets there and how it is retrieved.







Thanks for reading and click on my referral link. Well, damn it, for the sake of her, here heaped all this.








All Articles