Hjertnes.blog

#

November 22, 2017

It snowed tonight, the big question at hand: when will it rain and turn it into a living hell?

Intro to Express.

November 22, 2017

This is a super short, and super simple introduction to both Express and RESTful services. There will be details about both I won’t touch on. It is only intended as a starting point, and I recommend taking a deep dive into each topic on their own.

RESTful web services are a way to build web services. It is probably the most popular one today, and have been so for a quite a while. A web service is a collection of endpoints or URL’s you use to do stuff with your backend from either some kind of native app or even web apps. The reason we use web services is that it is a interface most developers are familiar with, and know how to do well and port 80 and 443 (http and https) are almost always open without restriction; this makes it a good option to make sure your users always can use your app or web app no matter where they are.

The core idea behind RESTful web services is that you have four different methods: GET, POST, PUT and DELETE. There are others but we’ll cover the basics with the four. But they should also be stateless. This means that except for saving stuff to URL and keeping track of who is logged in where, the server takes the input and returns the same data (based on what is in the database). There are no “you need to do this before you do this or the nature of one endpoints is completely different because of something you did before.

The four methods: GET, POST, PUT and DELETE are used for four different things. You use GET to retrieve information, POST to add information, PUT to modify information and DELETE to remove information. It is fairly easy to understand as long as you get those four principles. And you should also always use the most appropriate STANDARD HTTP Status code.

Let’s move on to Express.

My favourite thing about Express is how simple and how little extra stuff you need to deal with to get it up and working. It is the only REST API thing I have used and ejoyed where I don’t spend ages removing stuff before I can start.

The first thing we need to do in ordet to get express up and running is to initialize npm and install a few packages.

And then we’ll add some bare minimum boilerplate, so that we at least have what we need to retrieve post data.

What we are going to do now is to first look at how you define routes to deal with GET, POST etc and then look at the different ways you can send data to express, before we look at how to send data back. And a little bit about the method you use to do stuff in express.

If you want to add a method that goes something at a given URL in express (based on the code above) you can do something like this

You pick the URL endpoint your want, for examle /api/hello/world, and you give it a function, either a named or a anonymous one, that will do something at that endpoint. It takes two parameters, it doesn’t matter what you call it, but the standard is req and res. The first one is the REQUEST you recieve and the send is the RESPONSE you send.

There are three typical ways your endpoints will retrieve data (you also have headers and cookies, they are typical, but often very specific and not general). Url parameters, query parameters and body data. Body data are typically only used on POST and PUT requests, while the rest are available everywhere.

A url paramter looks like this ’api/hello/world/1, where the ‘1’ is the paramters. They are required and mapped in the url like this:

app.get('/api/hello/world/:id/', function(req, res){})

A typical use case for this is when you want to fetch, delete or edit a specific element. Compared to if you want to fetch all. Url parameters are available in the req.params object.

Query parameters on the other hand are often used for optional paramters (because it is a pain in the ass to add a billion different routes for url params). They also look different: ’api/hello/world?key=value&key1=value’. Everything after a ? is considered a query paramters; they are divided by a & and the key value pairs are divided by a =. Remember: they should be URI encoded. Ugly but useful. You find them in the req.query object.

And body data are available in req.body. Most of the boilerplate code above is code required to deal with posting data. Because you can post more than one kind of data as the body text. We used to post uriencoded data for a very long time. But these days we usually post JSON because it is way more powerful and easier to deal with.

The last thing you need to know is how you send data back to the requester. There are two methods I typically use when I send data back to the user res.send and res.status; the way I often do it these days is res.status(200).send(myListOrObject). You use .status to give it a appropriate HTTP Status Code, and .send to send some data. I usually use it to send JSON responses, but there are others to send other kinds of data.

I’ll probably come back and go deeper on Express in the future.

#

November 21, 2017

#

November 21, 2017

I’m really enjoying the audiobook of Joe Biden’s book.

jQuery in 2017

November 21, 2017

jQuery was THE SHIT like 10 years ago. I wrote my first large Single Page Application using it, and it still played a big role in writing SPA’s using knockout etc for a long time. But the big question is: are there still a place for libraries like jQuery in 2017?

I’m not sure if there is or should be room for jQuery with libraries like Angular, Vue or React; because of the differences in how they work with the DOM. With libraries like jQuery you treat the DOM as the “truth”, while with for example react you render things to the DOM from data in Javascript. Then you soon end up with some “interesting” results if you add some jQuery plugin or code that starts to change the code you just rendered.

But I do believe that there are many situations where React or Angular is 100% overkill, and a tool like for example jQuery is the right one because you just need to ajax and a few click events.

Should you spend time learning it? I wouldn’t. Learn ES2017, learn React etc. And I think most people will pick up on jQuery very fast, if they need to.

#

November 20, 2017

Hello World

#

November 20, 2017

Hello Spacemacs

#

November 20, 2017

So, I decided to finally write a emacs command to post to Micro.blog.

This is how it works: write what you want to post in a buffer, M-x (space space) post2mb.

First, open a terminal and cd ~/.emacs.d/private && wget raw.githubusercontent.com/tkf/emacs… && wget gist.githubusercontent.com/hjertnes/…

Then, open ~/emacs.d/private/mb.el and find the following line :headers ’((“Authorization” . “Bearer”)), you need to add a app token after Bearer; make sure there is a space between Bearer and your token.

Finally you need to open your spacemacs config: ~/.spacemacs, find the user-config and add the following two lines to it:

(load ”~/.emacs.d/private/request.el”)

(load ”~/.emacs.d/private/mb.el”)

Restar emacs an you should be set. Just open a new buffer, start writing and hit SPC SPC post2mb when you’re ready to post.

Fixing OpenSSL on High Sierra

November 20, 2017

I was coding some Chicken Scheme during the weekend and had some serious problems getting OpenSSL to work properly. This is probably the first time I’ve had to compile anything from source by hand in years.

The problem was that most of the stuff I use from day to day have migrated to use LibreSSL on OS X, while the Chicken Scheme eggs I was using hasn’t. Apple have moved over to Libre, and Homebrew have stopped to let me link OpenSSL, because of compability reasons. Or I do at least assume so.

I followed the steps on this StackOverflow https://stackoverflow.com/questions/38670295/homebrew-refusing-to-link-openssl thread to fix it

cd /usr/local/src

Run this if it doesn’t exist:

cd /usr/local && mkdir src && chown USER src && cd src

curl —remote-name https://www.openssl.org/source/openssl-1.0.2h.tar.gz

tar -xzvf openssl-1.0.2h.tar.gz

cd openssl-1.0.2h

./configure darwin64-x86_64-cc —prefix=/usr/local/openssl-1.0.2h shared

make depend

make

sudo make install

ln -s /usr/local/openssl-1.0.2h/bin/openssl /usr/local/bin/openssl

And, if you need to install the openssl egg (or any other SSL lib) run the following commands:

export CPATH=$CPATH:/usr/local/src/openssl-1.0.2h/include

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/src/openssl-1.0.2h/lib

chicken-install openssl

#

November 20, 2017

Android fans, I’m sorry, but I can’t stand material design. It’s not bad, just not for me.