Built a CRUD API for Robots with NodeJS, Express, and MongoDB

After building an API about myself I wanted to get into another API project right away. I found this article on Scotch.io about building a CRUD API using NodeJS, Express, and MongoDB. As I have been learning the MEAN stack I have put off working with MongoDB. In my previous attempts I had multiple issues setting it up and running it locally. I had enough issues that I put it off for too long. The Scotch.io tutorial mentioned a couple different online hosts for MongoDB databases, one of which is mLab. They offer a free instance that you can use as a sandbox database for development purposes. This was perfect to me as I want to learn how to interact with MongoDB, retrieving data and saving new data, but I’m not interested in learning how to admin or run a MongoDB instance. At least, not at this time. So, having a way to easily run Mongo and work with it was a definite catalyst to this project. It also lead to this tweet.

In the Scotch.io article it goes over the basic setup of Express and Mongoose. I used that as a template for getting my API up and running. It was a pretty straightforward process as I only needed one route with the appropriate handlers for each verb. Working with Mongoose was really easy and it worked to set it up on the route file. Working with the Mongoose docs made it easy to get my schema as ai wanted. In my API I used Robots as the example object I am creating, reading, updating, and deleting. I gave the object two custom parameters, designation and evil. Designation is a string and seemed more appropriate for a robot rather than calling it name. And it feels pretty important to know if a robot is evil or not, so I have that as well as a boolean. Each of these are given to the API via query strings. Being that I only have the two parameters I went with query strings in the URL instead of parsing out the body of the requests. In my next project I want to make a more complex object and that should lend itself to working with constructing custom objects to provide in the requests. Maybe that one can be superheroes. I can probably come up with a few more parameters for a superhero object.

It felt like the project went pretty smoothly and I was able to it up and running. I used Postman for testing out the routes and worked pretty well. I was able to run the requests as I needed and simulate a few error situations. I already know of one that I am not accounting for right now that needs to be addressed which is that requests will still go through to the POST route if neither of the parameters are supplied. It will end up with an empty object being created with no designation or evil params. I need to fix it so that the request is checked for both before I go to the database with nonexistent values.

Once that is fixed I will feel pretty good about the project. In fact I am likely going to fix it after I post this tonight. Next, I am work on an API for a more complex object. After that, I want to tackle adding in authentication so that I can have individual users. Right now anyone could use this API and it will all come in as one person.

If you want to check out the project you can find it on Github here. Feel free to let me know what you think in the comments here or submit PRs with your own ideas.

Some of my favorite definitions of NPM

NPM is super awesome and I totes heart it. First coming into the world of Node.js and seeing the power of installing modules to add new functionality to my projects I was super excited. It also made sense when I heard or saw people call it “node package manager” since it was for loading and managing packages in Node.js.

But, I quickly learned how it was much more than that and how that definition limited how one could think about NPM. Most obvious is that even though NPM is bundled with Node.js you don’t have to use it solely in Node.js projects. It can be used to manage dependencies of a purely frontend workflow. You can also find lots of awesome CLI tools for making your shell life easier as well. I am also a huge fan of the purely silly packages that exist to bring smiles to people’s faces. I keep thinking about how I need to write more of these.

Through posts online and such I have seen that the folks who maintain NPM also feel the same way, that NPM is so much more than a Node.js package manager. On the NPM homepage you can see in the upper left corner a playful definition of what NPM stands for. It changes every time you load the page. I recently found myself refreshing the page repeatedly and taking screenshots of the ones that made me laugh the most.

So I present to you, my favorite “definitions” of NPM.

screen-shot-2016-06-22-at-8-51-47-pm

screen-shot-2016-11-05-at-3-29-24-pm

screen-shot-2016-11-05-at-3-30-35-pm

screen-shot-2016-11-05-at-3-31-04-pm

screen-shot-2016-11-05-at-3-31-18-pm

screen-shot-2016-11-05-at-3-34-36-pm

screen-shot-2016-11-05-at-3-44-59-pm

Going back to bash-ics with dev tools

I recently nuked my entire development environment and I did so purposefully. As I have been teaching myself to write code I have come across many fun, shiny things that can make developing for the web easier, flashier, or something-er. As someone new to the whole world I just kind of grabbed everything because people said it was cool and would fix so many problems. I also installed things at the advice of others I met who said a particular tool made something really easy for them.

Over time this has gotten me lots of cool things, but in the end it is LOTS of things. I recently read an article on Hackernoon regarding How it feels to learn JavaScript in 2016 and it got me thinking about my own dev setup. If you haven’t read it already I would suggest it because it is funny and sadly accurate at the same time. After reading it I began to think about my own development process and how I would make an app today if I was working on a project. It also reminded me of how a few months ago I erased my system and set everything up cleanly on my Mac. When I did this I made a list of all of my tools that I would need to install afterwards. I took great care to make sure I got all of these “important” items.

As I mentally went over what I have installed on my computer I realized that if someone asked me why I use a particular thing that I don’t think I could truly defend my choice in using it. I would likely start with a story of how I found out about the thing and read or was told how it solves a certain problem. If I was asked if I had experienced that problem I probably couldn’t say so for sure. For example, I was using Oh My ZSH on top of zshell instead of using bash as my shell in my Terminal app, which was Hyperterm instead of Terminal that comes with macOS. I was told awhile ago that Oh My ZSH was an awesome tool with these great configurable features, but it required using zshell. So, I changed the shell on my system and installed Oh My ZSH without ever really using bash and seeing how they are different and if I actually prefer one over the other.

This is also true of NVM, another tool I installed after reading a blog post. I understand the benefits of having multiple versions of Node on your system in case you need to develop apps using a particular version, but I have yet to actually have that need myself. It will likely come as I work more, but until then I am leaving it off.

Now, I have my shell set to bash, hence the awesome and pun-tastic title of this post, and I’ve uninstalled all of the things I can’t justify using. Going forward I plan on working on projects and as I experience the pitfalls of a certain process I can look into solutions for that issue. But, until then I am going to work with a more default setup. One benefit to this is that to get my dev environment setup on another computer will just take a .bashrc and .bash_profile along with installing Atom and Node.js. The contents of those bash files are stored in Google Keep, so I can easily copy and paste their contents into place. Fun fact, I have already replicated the things I was using and like about Oh My Zsh and zshell in bash. So, I probably won’t be switching back unless something pretty drastic comes up. I’m also using Terminal over Hyperterm.

In the end this is meant to help me solidify my learning in web development so that I can speak directly to the stuff that I am doing and why I am doing it.

Teaching myself math from the beginning

Awhile back I read a book called Innumeracy: Mathematical Illiteracy and its Consequences by John Allen Paulos where they argue that a lack of understanding of numbers is just as detrimental as being unable to read or write. If you were to tell someone that you are illiterate they would probably be shocked and feel sorry for you. They would wonder how you get through life and probably offer help to find you a way to learn those necessary skills. But, the same isn’t true if you tell someone you aren’t good at math. “I’m not a math person” or “I’m not a numbers person” or something similar is commonly uttered by people and no one bats an eye. People often nod in acceptance or openly agree claiming the same for themselves.

But, not being good with numbers can have serious consequences on how we see the world around us. Things that are seemingly magical or special can actually prove to be statistically likely when observed mathematically. AsI have been teaching myself to write code I have begun to see things much more formulaically and I have come to see just how much I don’t understand in mathematics. Growing up I was much more into writing and other creative forms of expression. I did just enough as far as math was concerned to get by in school, so that I could move on to the next grade.

It is funny how as I have gotten older and working in code how much more I enjoy mathematical processes over creative ones. I do still think of myself as a creative person, but my creativity will likely get expressed more often within my love of technology.

Writing code has shown me how with a better understanding of math I could be much better at writing code. In some lessons I have found that it was a mathematical concept that got in my way rather than the code I was trying to write. For example, I was going through some algorithm tests and needed to write a function to produce the factorial of a number. I spent more time on learning what a factorial was than on the code that made it happen.

So, I have decided to go back and teach myself mathematics. Doing a cursory search online I have decided to go with Khan Academy as it is free and looks to have a lot of content. I am starting with their Arithmetic Essentials course. Getting a good foundation / reminder of the basics seems like the best place to start. Then I can get into the fun stuff after I remind myself of how long division actually works. Doing these courses has been humbling and eye opening to how much I rely on a calculator.

If you are interested in learning math better I would suggest checking out this article on how one person taught themselves math as an adult. It was a great starting point for me. If you have done something similar or have other resources you think I should check out I would love to hear from you. Let me know in the comments below.

Now, I should get on with my next lesson.

Writing an API about myself for my Github page

All Github users have the option to make a personal page ({username}.github.io) from a repo. I have long had one setup with no content in it whatsoever. To check it out in all of its blank white page awesomeness you can go here, if you really want. I’ve always wanted to use this page to make a simple portfolio for my projects and show off some of the things that I have done in the realm of web development. As I learn more and more about building things for the web I have come to realize that I really enjoy backend processes much more than frontend work, hence my focus on NodeJS. And since Github pages only support frontend things like Javacript, CSS, and HTML I haven’t really put much effort into designing this page.

But, I want to have something fun and interesting. The other day I had a great idea. I will write up a server that will return an object that describes me and the work that I have done. It will be a resume in a JSON object, a kind of API all about me. I can make the main endpoint return the whole thing and set other endpoints to the various sections of it. Then I can build out the page to make AJAX requests to this server. I will host it on Heroku likely, the server that is, and maybe even set a dropdown for the various endpoints with a button to send the request. Then I will display the raw object on the page. This is a project I can get behind and will actually want to build.

I will admit that I have seen this done before by others and I am no way saying that I am doing something unique here, but it will definitely be fun. One in particular I have seen and really like is one by Ashley Williams, who works at NPM. I figure this kind of project will turn out much better than if I just tried to make a fancy looking page to be flashy. I am even thinking of making it text only and I might have it display the raw object. This will speak much more to the kinds of projects I like to work on. As I have been writing this I am thinking about the various sections and how I could have them break down. One section could be for full fledged web apps and another for CLIs that I have made. This will be fun indeed.

Holy moly! I gave a talk about code I wrote!

As I have been teaching myself how to code I have been trying to go to various code meetups in the Seattle area. It is always fun to get into a room full of like minded individuals and hear people’s perspective on different web technologies. Most of them have talks of some kind where a couple people will present their idea on something in the realm of code. I have always wanted to give one and just recently got the opportunity to do it. I go to most of the BellevueJS meetups as they are usually pretty close to my work and are easy to get to after I leave the office.

A friend of mine, Matt, helps run it and after I put up my post regarding publishing my first package to NPM they asked me if I wanted to do a talk about that. I thought about it for a moment and said yes without really thinking about it. As I thought about it more the more it felt like a great idea. I have been wanting to do a talk about something and I could never really think of a topic. This would be a perfect subject for me to talk about as it is more about my getting over fears of putting code I wrote out into the world than the actual code itself.

So, I began preparing the presentation. I decided to keep it simple and had about five slides or so. Each one with a few talking points on them. I tried to keep it moving as I spoke and kept to the topics on the slides. I let the audience know that it was my first talk at the start which was more for me to get the nerves out than it was a disclaimer for them. It was a very receptive group and there was even a couple of questions for me afterwards. The group seemed to be mostly folks that are new to world of writing code, so it felt like a topic that resonated with them. At least it felt like it did to me.

I got some good feedback afterwards on the talk and on my package. Much like when I published the package it felt good to put myself out there. To be honest this was really more for myself than anything. Knowing that I could get up in front of a group of strangers and talk about code I wrote makes me feel really good about where I’m at as far as my learning in the field of web development. And also like the package, as simple as the presentation was, it felt great to get something out there. Now I can take what I’ve learned and use it for future things. I’m actually excited about the idea of speaking in front of people again. I plan on giving more talks about code and my views on it.

If you have ever wanted to give a talk about technology or really anything you feel passionate about I say do it. Take the leap and put yourself out there. It’s the only way to really know how it will make you feel.

I published my first package to NPM!

As I have been learning more and more about developing software for the web I have been wanting to be part of open source software and getting code I’ve written out in front of people. Part of that is writing about code here on this blog and another part is writing code and publishing it somewhere people can find it. I recently did a post about my plans to publish my first package to NPM and now I have done it, hooray! Just moments ago I published the package.

The package is an ExpressJS project boilerplate creator. It creates a simple directory structure along with some files to get started in creating an ExpressJS app. If you are interested in seeing it you can find the project here:

https://www.npmjs.com/package/express_project_boilerplate

I’ve done some basic testing and it works pretty well. There is on occasion errors that I have to look into, but running the package again seems to work. So, that should be fun to troubleshoot, yay!

But, really getting this project out in the world is really meant to be the catalyst for me to get over my fears of putting code I’ve written out there. It may not be the most sophisticated and it may not even be the best way to do this, but it is code I’ve written and it does the thing it is meant to do. That is pretty cool to me.

Let me know if you have any questions in the comments!

Deploying my first app to Heroku

I recently checked off a couple of goals for myself in learning web development, deploying an app to Heroku and building an app that uses the Smartsheet API. I was able to do it with one project, a Smartsheet based quiz game. It has a familiar style and game play to a certain well known and trademarked game show. I originally made this game as a simple HTML page using JQuery to hide and show the questions and answers. It worked for its purpose, but was difficult to maintain since the questions and answers were in the markup. If you wanted to edit the game you had to go into the HTML. I also didn’t have it hosted anywhere so to use it you had to share a folder and open the index.html file.

I started to think how it would be a great project for an app hosted somewhere that could have some kind of database of questions and answers to create a new game board each time you played it. Naturally, I thought it would be great if that collection of questions could be stored in a sheet in Smartsheet. At the time there wasn’t a method to use the Smartsheet API with JavaScript, but I found out that a JavaScript SDK was being built using NodeJS. So, I patiently waited and once it was available started working out ideas for how the app could work.

I took some time to get to know the Smartsheet API and how to work with it in Node. Once I felt comfortable I began working on the app. During this time I also worked on learning how to build apps in Express which I used in the project. It is an Express app built on top of Node with Jade, or I guess I should say Pug now, for the view templates. As I’ve been working on my learning to write code I have come to really enjoy working on back end development work over front end. The more I learn about HTTP requests and piping data into streams the less I am interested in writing click events or the hex values of colors. The process to pull the data from Smartsheet and randomly build the object for the game board was a really fun logic problem to solve.

But, I guess you can’t call yourself a full stack developer if you can’t write some CSS to go along with your POST requests. Once I had the project done and functioning it was time for the fun part of deployment. I chose to put my app up on Heroku. I had a little experience with deploying apps there before, but nothing I built myself, only apps from tutorials. It is really simple to deploy to Heroku as they make it really easy. I published the app and visited the url to see the dreaded “Application Error.” I should note that I attempted to do this deployment on a morning before work. I finished up the functionality and layout of the app the night before. I thought about publishing it then, but in the interest of getting a good night of sleep I chose to not do it. I woke up the next morning with it still on my mind and had the great idea of deploying the app over breakfast. I started troubleshooting the errors and almost forgot to go to work as I got so involved in research. I ended up bringing my laptop to work to finish it up in my free time.

Looking at the app I realized I hadn’t updated my port to the environment variable so that it loads to the assigned port. Instead it was still going to port 3000, which I was using for testing. I adjusted my app.js file and updated the app on Heroku, but still got the error. Looking at some support docs I found some mention of supported versions of Node and since 6.0.0 had just come out my version of 5.x didn’t look to be supported anymore. I updated my version of Node and the app still worked locally, so I pushed that change to Heroku. This allowed me to load the first page, but the game board failed with the same error.

Looking at the app logs I saw it wasn’t finding needed environment variables. I remembered that I am using dotenv to set some environment variables so that I can connect to the Smartsheet API, but keep them out of the files I am pushing up to Github. Call me crazy, but publicly displaying my API access tokens seems like a bad idea. Next, I looked up how to set the environment variables for the server for my access token and everything started working! Once the server could connect to Smartsheet for the data it was able to do everything it needed.

Now the game is up and running. It is awesome to think about how an application I built and designed end to end is on the internet. I definitely learned a lot in regards to Node, Express, and especially Pug (Jade?) since it was my first time using a template language to build out a front end. It also gives me a great idea of where I want to go next in my learning. Now that I’ve got a good understanding of Node and Express I’m looking to start learning Mongo and Angular to complete the rest of the MEAN stack. I’ve joked around with the idea of making apps that work with Smartsheet are like SEAN apps(Smartsheet-Express-Angular-Node). It would be awesome if that became a thing.

If you want to check out the Smartsheet quiz game app you can find it here:

https://smartsheet-quiz-game.herokuapp.com/

And the Github repo is here:

https://github.com/daveskull81/smartsheetQuizGame

I’d totally be interested in any feedback or ideas. I have a list of a few things to add for a next version, but it would be cool to hear what others have to say. Let me know in the comments! 🙂

Building my first NPM package

As I teach myself to write code I’m trying more and more to get involved with open source projects and to contribute my own code to the greater JavaScript ecosystem. NPM is an awesome resource I have used quite a bit in my own projects to find helpful packages and it’s time I start paying the community back with my own code.

Last Fall I decided I wanted to create my own package as a learning process, but I couldn’t really think of anything to build. I mulled over the idea and nothing exciting came to mind. I chose to not force the issue and worked on other things. I thought to myself that I should just work on other projects and as I work I should think about the processes I’m going through. If there is something I’m doing often by hand it could be a great idea to automate it with a package and if it is something I find useful others might find it useful as well. That would make a great idea for something to publish to NPM.

A few weeks ago I had an idea. I really like working with Express to build Node apps. It makes routing super easy and it’s simple to get something up and running. In the Express documentation there is mention of a project generator. I’ve tried it out and it definitely does what it advertises, but after working with it a few times I found I was editing the folder structure pretty extensively to have it the way I like working.

So, this will be my first foray into creating a package for NPM. I’m going to make my own Express project creator. I prefer a more stripped down directory structure and this will help me automate a manual task. Most likely, only I will find it useful as it will set everything up the way I prefer to work, but maybe others wil try it and like it too.

The true point of this project is to get over the nervousness I have of creating my first package and putting myself out there in the open source community. Using a simple project to get started will help me relax. Maybe then I will have an idea that might be more community driven to solve a problem I see others facing. But, that time is not right now.

I will put up a post after I publish it. Have you published your first package to NPM recently? Or are you a seasoned pro with advice for a newbie? Let me know in the comments!

I made a command line tool to lookup the weather

After typing in a zip code the results are shown on the command line.
After typing in a zip code the results are shown on the command line.

Now that I am putting the majority of my projects on Github I have been trying to come up with things to build. One of the first few things I set out to make was a command line app to get the current temperature based on a zip code. It is a simple node app that asks the user for a zip code, then looks up the current weather, and reports it back. I am using a couple of free APIs to make it happen. For the weather data I am using Forecast.io, but that requires a latitude and longitude to get the data. To get that based on a zip code I am using Zippopotam.us.
The basic flow of the app takes the user input using an npm package called Prompt and makes the request to Zippopotam.us. That returns a latitude and longitude for the zip code which is then passed into the call to Forecast.io along with a name for a city/town in the zip code.
The results are then printed out to the console in a basic sentence. I built something similar to this from a tutorial on NodeJS I did awhile back. I thought it would be a good starting point for getting up and running on Github as well as get me going on the “just build stuff” advice I get from basically everyone. I really liked the fact that this time around it was much easier to build and get working. I feel like I could have just built it in one sitting. I didn’t because I didn’t want to rush things, but it felt really good that this wasn’t as hard as I remember the tutorial being. It’s like I have actually learned a few things when it comes to building stuff in NodeJS.
If you want to see the code or even use the app yourself you can find the repo here. Feel free to submit a pull request if you come up with anything it needs.
I wouldn’t say this is complete, but it works pretty well. Let me know what you think!