RealWorld API – TypeScript, JWT, MongoDB, Express and Node.js

Hello, Welcome to my blog!

Recently, I subscribed to a tutorial website called Thinkster.io. Thikster has tutorials dedicated to making production-ready applications. They have tutorials for many different frameworks. I decide to go through their node.js tutorial.

I also wanted to get more comfortable with typescript, so I changed the tutorial up some by implementing the tutorial using TypeScript. The tutorial is written initially with regular javascript. I had the extra challenge of translating the tutorial to use TypeScript as I went through it. Going through this tutorial was a great experience. I learned some great techniques for building an API with node.js and TypeScript.

Programming with TypeScript was enjoyable. It cleaned up the code allot! Which is always a great thing. Since I have programmed in the past, with C, C++ and C#, I also found working with TypeScript very familiar.

I posted my code for this project on my Github. https://github.com/coreyjjames/REALWORLD-API-TYPESCRIPT

The tutorial also covered Postman. Postman is an application that allows you to test your API. I liked it allot, I have lightly used Postman in the past, but this time I used it extensively. I love how you can write test cases in Postman. It was handy at the end of the project to check for any bugs that got missed during the central development time.

In the next couple of weeks, I would like to improve this API some more by using a framework called GraphQl.

I’ll post another blog post when I finish!

If anyone has some improvements I can make to the code, let me know. 🙂

NodeChat Development

Have gotten a bunch of PR’s this past week, since this is the last week of class. Everyone is finishing up their release 4,  which means the project has made some great progress.

We now have the continuous integration system Travis, that helps with managing the app. And, we are working on es-lint and prettier, there is still some configuring to do but it’s almost up and running.

So with all the changes, there was a bit of backtracking I had to fix. One of the PR’s changed how the messages were sent to the screen but they forgot to change some corresponding code, so it ended up breaking the messages altogether so no messages would display correctly on the screen. It was a pretty easy fix, but I am not sure about keeping it. The change puts the JSX for a message into a variable, then we just render that. The problem with that is it doesn’t get updated anymore when state changes. I have to look into it more later, but I might have to go back to making the JSX just before it is rendered.

Here is the PR that fixed the above bug and a few others: https://github.com/OTRChat/NodeChat/pull/72.

The other bugs include:

  •  package.json – formatting bugs that formed when I did a manual merge.
  • Fixed avatar’s so you can now upload custom avatars. This change required changes to the server aswell, this is the PR that changed the server https://github.com/OTRChat/server/pull/4.
  • package-lock.json – There was a problem merging so I just deleted the old version and made a new one.

 

 

Finding a JS bug using chrome developer Tools

Another issue was filed in the cube-roll project, Issue #4 which was a problem with the games score not updating. It turned out to be a small fix but I wanted to go through how I found where in the code the bug was.

I first started the game and duplicated the problem.

I then looked through the code and find what I think gets called when I get a point. I found that line 90 of the world.js file is where the logic for updating the score was.

After finding the area of code where I thought the problem could be, I opened the developers tools in chrome and then I navigated to the sources tab. In the sources tab I navigate to the file I want world.js. I can now setup breakpoints, after that I started the game and duplicated the problem again. This time since I setup the breakpoints the game pause on the breakpoint and I could look at the values of the data in the developer tools.

As you can see in Figure 1 this is what happens when you trigger a breakpoint.

A snippet of the chrome developer tool
Figure 1

After doing the above a few times in multiple areas of the code, I located the line of code with the problem. Inside, hud.js there is the function setText() that is called from line 98 of the world.js file.

Inside setText() I found the below typo:

setText(id, text){
    this.elements[id].test=text; // .test should be .text
    this.redraw=true;
}

Now that I found the problem I was able to fix the typo and submit a PR:

https://github.com/mklan/cube-roll/pull/5

Continue development of chat app.

Development on the chat app has been going good. Few things I would like to get finalized would be a continued integration system, linting, tests and database.

I think travisCI would be a great addition to the repo. Took a class today that explained how to implement travisCi into a GitHub repo. It looks like it will be pretty easy, might add it this week.

To make travisCI really useful it need’s to be doing more then just running the app as the test. It is capable of running linting software like eslint and actually test that could be created for the app.

I would also like to start research on the best options for incorporating a database into the app. I created a discussion thread here, I would like other people’s opinion in making this choice.

Also, for release 3 I found a external project to contribute to on GitHub. It was interesting, it required me to get into a bit of Ruby which was interesting. Here is the PR made for the code-gov-style project.

Maintaining a GitHub Project

Wow I now have a much better understanding of the life of a project maintenance, after this week.

So, a few classmates have now joined the NodeChat Project, the week started simple by me posting a bunch of issues for everyone to work on. But, once they started to work in the project, they started to post their own issue’s. They ran into thing’s that being the one who wrote most of the code, I didn’t really think about.

For example, the readme file I didn’t need it so it was very outdated and simple. So, I needed to update that so people could more easily start working. Also, I added a contributing file which explain’s a few more thing’s. updated docs.

Another example, was the folder structure was outdated, we were working inside a folder while the root contained the original project before I started working on it, so there were a bunch of files kinda not really doing anything. And we also had the server inside the same git repo. Apparently this is very confusing, another thing that I got used to and learned to ignore, which wasn’t good for new contributors joining the project. We have now gotten ride of all the old code and moved the server into it’s own repo. So now the react app is alone it the main repo, which make a lot more sense for new contributors.

Lot’s of change’s happened over this past week on the OTRChat Project. I have now had a taste of what managing a project looks like and I am only working with 4 people, can’t image large project with 100+ contributor’s. As with everything you start small and work toward something big.

 

Release 3

So far for release 3, I have been working on preparing the NodeChat repo for my classmates.

I have been working with the owner (Josh) of the repo to get permission’s to the Nodechat repo in this issue. He has been very helpful, I now have the required permission’s to edit the repo and he gave me some direction for the project!

I also had to fix a few things, before everyone forked the repo. I needed to merge my previous PR’s. Josh merged 2 of them. I needed to fix some merge conflicts on the other one, so I did that and I also put up a new PR that fixed a few bugs that were created by the merges and update the readme here.

The repo is now in a state that my classmate’s can start working on it. I posted a few issue’s up for people to work on, I am going to add a few more soon. Josh gave some ideas i need to look into.

Hacktoberfest 2018 – My experience

Hacktoberfest 2018, was a great learning experience for me. I was able to find a great project for this event, that fit my interest’s and ability’s.

Starting this event was a bit challenging, I knew I wanted to work with something with java script. And I also, wanted to work on more front end development. So this helped me narrow down what project to work on, picking a project to work on took some time. After looking through the Hacktoberfest tag on GitHub for a while I came across the Node Chat app that was using node and socket IO to create a chat app. The owner of the project wanted to recreate the test site using react. This peaked my interest, since I was familiar with react, node and socketIO,  so I took on the task of creating this react site for my first pull request for Hacktoberfest 2018.

After my first PR on the Node Chat I asked to continue working on the react app. The owner of the app invited me to be a maintainer for this project. So, for the rest of Hacktoberfest I worked on bugs and features of the react app.

My Hacktoberfest 2018 Pull Requests and Issues

  1. https://github.com/joshghent/NodeChat/issues/11
    https://github.com/joshghent/NodeChat/pull/24
  2. https://github.com/OTRChat/NodeChat/pull/27
  3. https://github.com/OTRChat/NodeChat/pull/28
  4. https://github.com/OTRChat/NodeChat/pull/29
  5. https://github.com/OTRChat/NodeChat/issues/1
    https://github.com/OTRChat/NodeChat/pull/30

Overall, I had a very positive experience in Hacktoberfest 2018. I learned allot about git, especially using branches. I now use branches all the time for whenever I want to test something or work on a issue or feature. And after making 5 PR’s I am much more comfortable with the process.

Now that I know events like Hacktoberfest exist, I am definitely going to keep an eye out for them, in the future.

Hacktoberfest Week One

In my first week of Hacktoberfest I started off without any particular project in mind. I had a few ideas of what I wanted to work on, so I started by searching GitHub for a project. I was looking for something using JavaScript or C++.

After searching for a while I came across a application called NodeChat. In Issue #11 for the NodeChat project the owner asked for someone to move the public site over to React. Having used React, Node.js and Socket.IO I asked in the issue if I could do this.  After, that I started my work on the React app to replace the JQuery version that was previously used in the project.

Making a chat app using React

I started off with some project designing. I needed to figure out how I wanted to organize the react app. Also, I had to go through the existing JQuery file to get a better understanding of what the project is doing.

I decided on the below structure for the app.

    • App
      • This component is the core of the app were the Login Page is called and is currently were all the css and assets are located.
    • LoginPage
      • This component handles new and previous user login’s.  And handles displaying the login page or the chat page.
    • ChatPage
      • This compenent handles all the functionality of the chat.

Currently the flow of the app is as follows the app page displays the Login page. The login page creates a new user or uses a previous user then the Login page decides when to display the Chat page.

A update I think I will do at some point would be to make it so the App page receives props back form the Login page. That would instruct the App page when to display the Chat page. I think this would be a bit cleaner code.

There is still a bunch of stuff that need’s to be finished but at the moment the app is working on a basic level. I am hoping to get most of the bugs fixed as soon as I can.

I think this will be a good project for me over Hacktoberfest and probably after.

Here’s the link to my first PR of Hacktoberfest .

Below is the 2 class definitions for Login Page and Chat Page and I added a small comment about each function. Full Code available here.

class LoginPage extends Component {
    constructor(props) {}
    // Checks if user pressed enter key to submit there message.
    enterKeyPress(e) {}
    // Used to manage the State of the input field on the Login Page.
    setUsernameInput(event) {}
    // Calls checkIfPreviousUser()
    componentDidMount() {}
    // Sets the username 
    setUsername() {}
    // Checks current user is a previous user or calls setUsername() if not.
    checkIfPreviousUser() { }
    // Tell the server that we've had a user join
    userJoin() {}
    // Holds the content's of the Login Page
    displayLogin() {}
    // Render Login Page if not loged in or Chat page if Logged in
    render() {}
}

class ChatPage extends Component {
    constructor(props) {}
    // Calls greetUser() and Lisen's for New Chat messages.
    componentDidMount() {}
    // Displays Greeting message.
    greetUser() {}
    // Checks if user pressed enter key to submit there message.
    enterKeyPress(e) {}
    // Used to manage the State of the input field on the chat app.
    setChatInput(event) {}
    // Send's the message to the server.
    sendMessage() {}
    // Recieve's the chat messages from the server.
    addChatMessage(data) {}
    // Used to handle images(not tested).
    parseMessageText(inputString) {}
    // Used to handle images(not tested).
    imageFile(filetype) {}
    // Used to handle images(not tested).
    getFileType(inputString) {}
    // Used to handle images(not tested)..
    imageLink(inputString) {}
    // Used automatically scroll the page.
    scrollToBottom() {}
    // Render page content.
    render() {}
}