Let’s build a content extract endpoint — part 2

netsi1964
3 min readApr 1, 2018

The idea has been defined (see part one of this post) now let’s try to construct such a service.

I want these things to be used:

  1. Hosted on Heroku (it’s free)
  2. Build using javascript — node (it’s free)
  3. Use Express webserver (it’s free)
  4. Put the code on Github (it’s free)

Create a repository on Github

I start by creating a new project on Github and clone it to my computer using Terminal git clone. I am use this post “Creating a simple REST API with expressJS in 5min” to install express.

Use Express server

I use Microsoft Visual Studio Code as editor. In there I have a terminal window integrated, nice as I do not have to leave the editor when I have to do things like initializing the code using “npm init” (If you want to use NPM packages you first init you npm package in that way).

The javascript to do the job

Now we have a project, hosted on Github, using a javascript based Express web server:

  • The web server is the outer layer of our application. It will take any requests.
  • Parse the request into our code.
  • Our code will do something and then hand over some data to Express which will then return the response to the requester.

It is basis web server design. Now what should happen in each step?

The web server needs information about which endpoints it should use. Let’s use the root url as the primary endpoint. That means that our code should be used if a request for “/” is sent.

I for now will let the response be a static response. So I change the code in the routes.js to respond with this:

res.status(200).send({ total: “4,793”});

Let’s connect the code with Heroku

The code is now ready for its first test on an actual URL on the internet. I use Heroku in this project to host my code.

Some setup is required, I will skip it here, but you can see what I saw:

The Heroku setup (part of it)

I deployed and went to see it running… but some error occured… I know, I had hardcoded the port number, and that is not okay as I do not know which port Heroku will serve it on.

I found one example about how to handle dynamic ports here: https://scotch.io/tutorials/how-to-deploy-a-node-js-app-to-heroku, commited the changes and deployed the app again. That helped!

My basic endpoint is now up and running here: https://extract-content.herokuapp.com/

First response from my Express, node, Heroku, javascript based web service!

In part 3, I will look into making the endpoint accessible to scripts and parsing the parameters, and will create a pen at CodePen.io to test the endpoint.

Part 3 ->

--

--

netsi1964

Keywords: Open minded, cross-think. Able to see soloutions from one area being usefull in another area. Please inspire me! :-)