Backend Knowledge Sharing #13

Puncoz Nepal
YoungInnovations' Blog
4 min readAug 21, 2019

--

GraphQL, Deploy node app using Capistrano, Webserver over NAT network.

Backend Knowledge Sharing #12: GraphQL, Automated deploying Node app using Capistrano, Webserver over NAT network.

Table of Contents

  1. Getting started with GraphQL in Laravel
  2. Automated deployment of NodeJS app using Capistrano
  3. Webserver over NAT network: hosting websites from home computer.

Getting started with GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

A simple query using GraphQL is shown below.

{
user(id:5){
id,
name
}
}

The output of the above query is:

{
data:{
user:{
id: 5
name:'harry kane'
}
}
}

Basically, GraphQL is used to overcome the over flooding of the data in API. Its main idea is to “Ask for what you need, get exactly that.

Rapid GraphQL Development with Laravel using Lighthouse

This week Ashish Shakya shared his understanding of GraphQL along with basic implementation in Laravel app using Lighthouse package.

Lighthouse is a PHP package that allows you to serve a GraphQL endpoint from your Laravel application. It greatly reduces the boilerplate required to create a schema, integrates well with any Laravel project, and is highly customizable giving you full control over your data.

All the required steps to include GraphQL in Laravel project is described well in its official website.

Also, for testing and exploring schemas one can try Laravel GraphQL Playground for more UI experience.

Automated deployment of NodeJS app using Capistrano

We have been using Capistrano to deploy our web-based application in our servers. This week Puncoz Nepal shared how we can deploy NodeJS app in the server using Capistrano, especially when we have server’s user-specific node running via nvm (Node version manager).

NVM usually get triggered from shell profile, which will not run when we use Capistrano. Hence, to run node or npm in Capistrano, first, we need to run nvm script and then node or npm commands should run. Generally, the following commands should execute for nvm.

source "$HOME/.nvm/nvm.sh"
nvm use $NODE_VERSION

There is a plugin for Capistrano, capistrano-nvm, which supports nvm for Capistrano v3.x and this plugin runs nvm before executing node related commands exactly as I mentioned above.

Another blog post with a detailed procedure to deploy a NodeJS app using Capistrano is being published in a few days. Stay tuned for the updates.

Webserver over NAT network: hosting websites from home computer

Ever wonder hosting a website from your home pc or some small embedded computer like raspberry pi over a NAT network, a network without static public IP? It seems possible but there are certain factors which make it difficult to achieve that goal. This week Bikram Tuladhar shared his workaround to web hosting from raspberry pi which is on his home network.

He presented how to use remote port forwarding which is also known as reverse SSH Tunnelling to make a connection between a home server and the remote server.

In the above diagram, the home server is creating a tunnel with a webserver on port 80 to remote server’s 8080 port which will accept and relay the connection from remote to home and vice-versa. The syntax for reverse ssh tunneling is,

ssh -NR 8080:localhost:80 user@remote-server-ip

here, N= do not execute the remote command and R=remote SSH port forwardings.

This command will make a connection between the home server’s port 80 and remote’s port 8080 which we will use it in Nginx as a reverse proxy to accept request from website. Nginx reverse proxy config is given below,

server {
server_name blog.domain.com;
listen 80;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;
}
}

Voila, now, when we visit the http://blog.domain.com, Nginx will receive the request and pass it to localhost:8080 which is tunneled with home server’s 80 port.

--

--