Database Schema and API Finally Working Together

I’ve gone back and forth with myself on this topic because it was a tough problem to solve. Initially I decided on MySQL because I wanted a human readable schema, and JSON can be kind of difficult to read. However, a lot of the solutions I came across for handling JSON in Node.JS with MySQL were unhelpful. Some were sort of wrong, and others could not have been further off base. Since MongoDB is so popular with Node.JS developers I decided to give that a shot, but its test environment design does not work well for my style. I decided to go back to MySQL, and now I have a full API that is easily retrieved.

The thing is that res.json() can send a mysql query result as json to the server. If you use JavaScript’s fetch() method it is very easy to retrieve the information. You can even easily and directly store the information in HTML with the fetch method. One important caveat with this method is that JavaScript and NodeJS do not always execute code sequentially. As a result, whenever you have multiple portions of a query you want to pass it needs to be done all at once. The following code snippet is still not finalized but this is how you can use fetch to process API requests in JavaScript. The overall result is very similar to what I was doing with vanilla JavaScript, but with far less code.

Leave a comment

Your email address will not be published. Required fields are marked *