Learning Node.js 002 – Exporting Projects from Glitch.com

For the last couple weeks I’ve been playing around with Glitch.com. I don’t have much experience with Node.js, so it’s been a good way to learn more about Node.js without having to setup anything locally.

Glitch is great because you can learn a lot by checking out other projects and remixing their code. That being said, there is a pretty steep learning curve with Node.js.  Just to get started, you need to have a pretty good understanding of JavaScript and then you need to learn about npm, view engines, package.json files and so much more.  Glitch.com makes this easier, but there’s still a lot to learn. 

Glitch’s web based code editor is pretty good, but it’s still not the same as using a local editor, so I decided to export my project and develop it locally. It’s easy enough to export a project, but the exported project didn’t work right away.

Starting the project, I got the error:

Error: OAuth2Strategy requires a clientID option

I’m using the passport-oauth package and I discovered that the secrets I saved in the .env file on Glitch weren’t being loaded in my local environment.  Instead of using the .env file in the project, Node.js uses the variables in the .bash-profile file.  You can view those environment variables by adding this line to your project:

console.log(process.env);

To fix the issue with the project exported from Glitch, one option is to move your secrets to the .bash-profile file, but that can get messy, so another option is to use the dotenv npm package.

Add it to your app with:

npm install dotenv --save

And then ensure that it’s referenced in your project with:

require('dotenv').config();

Learning Node.js 001 – Starting an App

Once a shell of an app is created, it can be started with the command node app.js  (assuming that the app’s main file is app.js. Other common names are server.js and index.js)

The alternative way to start an app is with the command npm start. By default npm start will fill look for a start property in the package.json file. For example:

"scripts": {
    "start": "node app.js"
  },

If the package.json file doesn’t contain a start property, npm start will default to node server.js.  Note: this will only work if your main file is called server.js.

The first time I tried using npm start, I got the following error:

npm ERR! Invalid name: "Vert Tracker"
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/kevin/.npm/_logs/2018-05-23T22_09_11_616Z-debug.log

Digging around I found that the name property of the package.json file cannot contain spaces or capital letters. After I changed the name to “vert-tracker”, npm start worked. There are other tips on the package.json npm page.

Learning Node.js

I’m slowly attempting to learn Node.js. To do that I’m reading Getting MEAN with Mongo, Express, Angular, and Node, playing around with apps on Glitch, and creating a mini-app. I learn best by jumping in head first so I’m finding it helpful to write code for a real app even though I feel lost half the time. 

Node.js has a pretty steep learning curve, so I’m planning to document some of the challenges that I face along the way, even if it’s a simple bug that takes me a few minutes to figure out. The primary purpose of writing these posts is for my own future reference and to help me internalize the lessons I’m learning.

Reset Mac OS X Dictionary

I’m heavily reliant on the Mac OS X spell checker, but I sometimes click Add to Dictionary instead of the correct spelling. For obvious reasons, that isn’t good, so it’s helpful to know where those new words are stored. Here’s an example:

Screen Shot 2016-09-01 at 10.22.04 PM

You can view the custom dictionary by opening Finder and navigating to:

Users/USER_NAME/Library/Spelling/

In that directory, you’ll see an en file and a LocalDictionary file. You can open each of those files with the TextEdit.app and view the words that were added to the dictionary. Embarrassingly, my Local Dictionary file currently looks like this:

Screen Shot 2016-09-01 at 10.33.01 PM

Delete any unwanted words and save the file. The changes will take effect once you reboot.

Current as of Mac OS X 10.11.6 (El Capitan)

AppleScript – Executing JavaScript in Safari and Chrome

I haven’t used AppleScript much, but I was recently working on a script to automate a workflow and I was surprised to discover that each browser uses different commands to execute JavaScript.

Specifically, Safari uses do JavaScript "add code here" in document X and Chrome requires execute javascript "add code here"

Here’s an example of each:

Safari

tell application "Safari"
	open location "https://google.com"
	if (do JavaScript "document.readyState" in document 1) is "complete" then set pageLoaded to true
	display dialog pageLoaded as string
end tell

Google

tell application "Google Chrome"
     open location "https://google.com"
     if (execute javascript "document.readyState") is "complete" then set pageLoaded to true
     display dialog pageLoaded as string
end tell

A couple notes:

  1. AppleScript.app autocorrects the second example to a lowercase JavaScript, but either should work.
  2. Safari requires that a document is specified
  3. If you’re using Firefox, it’s currently not possible to execute JavaScript with AppleScript

Man-Made Wave

Man-made waves have existed for many years (as seen in the classic 80’s movie North Shore), but most of us doubted that we’d ever see anything that resembles a real wave.

Kelly Slater’s company has been working on creating a man-made wave for years and they recently released a video of a wave that shows a lot of promise.

Below is a video of Josh Kerr on one of his first waves in the new wave pool.

Rumor has it that the wave pool is in the Fresno area. That’s only a couple hours from where I live, so I hope the rumors are true.

They haven’t released any other other details, but it’ll be interesting to see how they structure the pricing and control the crowds once it opens to the public.

Only the Essential: Pacific Crest Trail Documentary

Yet another Pacific Crest Trail (PCT) documentary to add to your list: Only the Essential: Pacific Crest Trail Documentary

The film was created by Casey Gannon and Colin Arisman during their 2013 thru-hiked of the Pacific Crest Trail.  The film is short (less than 40 minutes) and does a great job of capturing the spirit of the trail.

The narrator’s voice was a bit monotonic and some of the stop motion videos were nauseating, but overall I enjoy it. They captured some amazing footage of desert and the mountains and it was refreshing to see their enthusiasm throughout the journey. Even though hikers are documenting their trips in increasing numbers, I still enjoy watching most of them and see new facets of the trail in each one.

It’s embedded below, but you can also check it out on the Wild Confluence Films site.

Backpacking Documentaries

I’ve watched a lot of backpacking documentaries, but I think I have a new favorite: Do More With Less | A Conversation About The Pacific Crest Trail. I highly recommend it if you have any interest in backpacking or the Pacific Crest Trail. Here’s the full video:

Next up on my list is Tell it on the Mountain. The video download costs $15, but here’s a trailer:

If you have any favorite hiking or backpacking documentaries, please let me know.

Chrome Redirect Cache

I recently created a new site, but I visited the domain before the site and was redirected to a cgi default page. And as a result Chrome cached the URL as http://example.com/cgi-sys/defaultwebpage.cgi

After I got the site up and running, Chrome continued to redirect the domains to the cgi default page even though it was working properly in other browsers and an incognito Chrome window.

After a week or so of this annoyance, I searched the web for a solution and I found one on Sal Ferrarello’s site. It turns out that Chrome caches redirects and there isn’t a clear way to clear the redirect cache.

The solution that worked for me was Sal’s first idea to disable the cache from the Chrome Developer Tools. The easiest way to open the Developer Tools is Command Option I

Once the Developer Tools are open, click on the settings icon shown here:

Once the settings modal opens, check the box next to Disable Cache (while DevTools is open) option as shown here:

disable-cache

Now visit the page that is cached and the old redirect cache should be permanently removed.

Check out Sal’s post for more details.

Unnecessary Medical Care

A few years ago Atul Gwande wrote about the medical cost conundrum in the U.S. He follows up on that article with Overkill. This article is an in-depth look at the harm caused by unnecessary medical care.

I’ve become cautiously skeptical of most medical advice and this article reinforces much of my skepticism.

Here are a couple of interesting quotes from the article:

Doctors generally know more about the value of a given medical treatment than patients, who have little ability to determine the quality of the advice they are getting. Doctors, therefore, are in a powerful position. We can recommend care of little or no value because it enhances our incomes, because it’s our habit, or because we genuinely but incorrectly believe in it, and patients will tend to follow our recommendations.

The forces that have led to a global epidemic of overtesting, overdiagnosis, and overtreatment are easy to grasp. Doctors get paid for doing more, not less. We’re more afraid of doing too little than of doing too much. And patients often feel the same way. They’re likely to be grateful for the extra test done in the name of “being thorough”—and then for the procedure to address what’s found.