Blog

How to add a subdomain to an existing certificate using certbot

You’re probably reading this because you already have a Let’s Encrypt certificate issued using certbot for a domain, and now want to add-on subdomains.

If you want to create a new certificate, have a look at the end of this article.

In my case I already had a certificate which covered

  • golightlyplus.com
  • www.golightlyplus.com

I have a directory on my server called dev that I want to reference via dev.golightlyplus.com.

So to do that, I issued the following command

certbot -d golightlyplus.com,dev.golightlyplus.com,www.golightlyplus.com --expand

Key takeaways:

  1. re-include every domain name you want on that certificate again. I forgot www. the first time. No biggie, just re-run with all the domains you want.
  2. don’t forget to add --expand to the end

After reading around on the net a bit, I found this solution on this GitHub thread.

How to automatically and permanently delete unwanted emails

Gmail provides the option to filter unwanted emails to your Bin. Click here for the instructions on how to do that.

The emails though only get automatically permanently deleted after 30 days.

So if you are trying to block someone from emailing you, and you have weak moments and end up checking the trash, you kinda still haven’t removed the temptation properly. Or are not able to put up effective boundaries.

So I built a system that allows users to register and automatically clear their Bin folder every hour.

It’s one of the tools available at my new site https://loveyourself.io/

Try it out and let me know how it goes!

 

 

How to setup a Node.js server on DigitalOcean

Once your droplet is created and you’ve ssh’d in, it’s time to set some things up..

I start off by installing nvm. You can find the command for the install script here. At the time of writing, I used

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

I needed to exit my terminal, and then ssh back in.

Then, for me, I want the latest LTS from Node.js installed.

So I run nvm install --lts

Also  I want the lts versions to be my default node with nvm.

So type nvm alias default lts/*

As a personal preference I then clone out my repo to /var/server

Once in there, of course run npm install

Then I like to setup forever, so I can make sure the server runs indefinitely.

npm install -g forever

Then it’s just a matter of starting up your server. So something like

forever start my-server.js

How to setup Nginx for WordPress on DigitalOcean

I decided to switch from Apache to Nginx.

I have a couple of WordPress sites on a DigitalOcean droplet, so I needed to reconfigure them to be used with Nginx. I basically followed this guide which was great for most of it, but I was still not getting the sites loading properly (404 errors, forbidden).

I needed to tweak the server block files to get it working, and this is what I came up with..

In /etc/nginx/sites-available create a separate file for each domain you’re hosting. For one of mine, I called it goforself.me to correspond with my Go For Self website, and this is the contents of that file…

Then in /etc/nginx/sites-enabled/ create a symlink like so

ln -s ../sites-available/goforself.me .

I like to have my sites being only accessed over HTTPS, so there are a couple more steps to get this going. I basically followed this guide, and this is the essence of what needs to be done..

First install certbot

For my Go For Self website setup above, I then type

sudo certbot --nginx -d goforself.me -d www.goforself.me

and I choose option 2 to always redirect to HTTPS.

And that is pretty much it. You’ll need to restart nginx to get it going.. so I go

service nginx restart

Any questions, let me know!

How to get a new Google access token from a refresh token on Node.js

If you’re creating an app where you need to access Google services from a server, you’ll need to store the refresh token from the Google Oauth process. The access tokens periodically expire, so you need the refresh token to manually get a new access token when you need it.

So if you’re using the Passport auth middleware for Node.js you would have something like this to make sure you get the refresh token. Store this refresh token in a database.

Then when you need an access token to access gmail in this case, you could get a new access token like this

— based on these Google Identity Platform docs

How To Use jQuery To Post A CSV File To A Node.js Server

If you have a front-end that needs to accept a CSV file, upload it to a server using jQuery, and then have that Node.js server process that CSV file, then this a way to do it.

First up, the HTML

and then the jQuery that takes the selected CSV file, and uploads it to a server

and finally the Node.js file that will accept and process it using Express (with the multer middleware), and then iterate through the CSV file.

As it turns out, the Node.js CSV libraries I found on npm were either not working or hadn’t been updated in a long time. To process a CSV file with standard JS using functions like split and replace made this relatively easy to do on my own.

Any questions let me know!

How to grab the page source from any dynamically generated webpage and then process it

 

I had an issue where I needed to scrape some data from a dynamically generated webpage. Open this page in a browser and you’ll see what I mean.

I tried a lot of options, and the solution that ended up working for me was to use Headless Chrome. This basically allows you to launch Chrome and use it as a tool from within your code. It’s recommended to install Chrome Canary and you can get it from here.

Essentially the pseudo code for my script goes as follows..

  1. launch Chrome in headless mode (no visible window)
  2. load a page and wait until the page is loaded
  3. then wait a little longer until all the JS on the page has completed
  4. then click on something using a querySelector to change the language to English
  5. then grab all the source code from the page
  6. then load that source code into cheerio and perform queries as needed

The full source code for this is below. You’ll of course need to install the 3 packages too.

> yarn add chrome-remote-interface chrome-launcher cheerio

I hope that helps some people! It took me ages to finally get to this place where I could scrape a dynamically generated webpage from Node.js.

How To Conditionally Remove The PayPal Payment Gateway In Shopify

So I recently had a job where we needed to remove the PayPal payment gateway in the Shopify checkout for particular products.

So a simple way for the client to manage this, is for them to tag those products they don’t want to be purchasable by PayPal, and then for us to write a script that detects those tags for those products in the checkout, and then removes that payment gateway.

Now it’s not possible to modify the checkout experience for standard Shopify stores. But if you’re a Shopify Plus customer then you can use the Script Editor App which does allow you to programatically modify the checkout experience. The API for this is here.

Now my language of expertise is JavaScript, and to use the Script Editor you have to use Ruby. This wasn’t actually too bad to learn. As someone that has done a lot of programming, looking up the syntax online for how to do something was quite easy. Particularly with the help of some Ruby pros in slack chats.

So, back to the problem at hand. If the client just added a tag called ‘no-paypal’ to whatever product they wanted to trigger the removal of the PayPal payment gateway, this is how we did it:

A gotcha I had trouble figuring out on my own: when you create a new script in the Script Editor, be sure to choose the correct script template. This is because the different types of scripts have different data objects available to them at runtime. In this case, I thus needed to choose the “Payment gateways” template.

Any questions/comments, let me know!

How To A Create A JSON API Wrapper For SOAP

I had a project where I needed to setup a server that communicated over the SOAP protocol. I use Node.js for my backend server, so I began looking for libraries to use to work with.

I settled on strong-soap, but began to have issues with doing simple changes to the XML like editing the prefix for the namespace. I opened an issue for this, and there didn’t seem to be an easy way to remedy it.

It turned out the easiest and most robust solution was to explicitly write out my own XML and use request-promise-native to do a POST request with that custom crafted XML as the body to the server I was trying to communicate with.

Using ES6 I could use string interpolation to insert the values into the XML from the express server I set up to wrap around the SOAP request.

So essentially I could make POST requests to my server using JSON, and get a JSON response. And voilà, a JSON wrapped interface for a SOAP protocol.

A somewhat cut-down version of my actual code (that includes setting up https as it seems ever more important) is this…

The main advantages to this approach are:

  • No extra libraries needed
  • I can be 100% confident in what XML I am actually sending to the server. I had the SOAP 3rd party asking for the exact XML fragment in order to help troubleshoot issues with me. Using strong-soap I wasn’t sure as it was generated.

Any questions or thoughts let me know! Thanks.

How To Create Your Own Currency Conversion App

For a recent Shopify job, I had to update a variety of prices by converting them to a different currency.

I found this great API from Fixr.io. It’s super fast to use (response times in less than 7ms), and responses themselves are simple JSON objects. The exchange rates are updated almost every day from the European Central Bank.

To demo it to you, I decided to build a currency conversion app that converted USD to NZD. You can view and play with this app online here.

The source code for it is all on GitHub as a gist:

What I realised that’s kinda amazing after I built this, is that a currency conversion app can be built in less than 75 lines of code. And that includes all the comments and styling. I also used the numeral.js library to format the numbers and jQuery for the DOM updates and Ajax request.

And, here is a video overview of the whole process you can watch:

 

Any questions or comments let me know! Thanks!