Spencer Pollock | Blog

Simple Express API

April 22, 2020

Without going too far into what's going on, I'd just like to preface this post by saying; I hope you're all staying safe, keeping active, and working to be a better you during this pandemic.


As time flies indoors, I'm trying to expand my skillset. Today, I looked at ExpressJS, a fast, unopinionated, minimalist web framework for NodeJS.

At the time of writing this, ExpressJS 4.x is the latest version with 5.x in alpha.

As some may know, I finished my degree with a project written in TypeScript (see that project here). This just got my feet wet into the water of the JavaScript community pool. There is still lost out there I have no clue about, but I hope to expand my web skills, both front-end and back-end, to give some live examples of not only JavaScript related content, but also other projects and work.

Express quickly

Express is a framework to create web applications, APIs and more with a focus on performance.

Instead of using purely NodeJS - where you would have to write your own parsing of payloads, cookies, storing sessions, selecting correct routing patters - ExpressJS handles this all for you with less code you have to write.

Express is primarliy used for handling requests and handing off data. It is not an MVC - that you have to bring yourself - but can serve static files that are pregnerated to end users.

For this example, we will be focusing on an API end point with Express.

Express can also server static files and has a fancy tool called express-generator that helps scaffold a simple site with one of the predefined static site engines.

The Example

Firstly, a few prerequisits are required to get going:

Install NodeJS

Get it right here -> NodeJS

Check the installed versions (nodejs/npm) with:
$ node -v; npm -v

Create a new directory

$ mkdir myapp; cd myapp

  • Make the directory
  • Change Directory

Create a npm project and a git repo locally

$ npm init --yes; git init --quiet

  • Initialize the npm project with defaults
  • Initialize the git repo and suppress output

Install ExpressJS

$ npm install --save express

  • Install the express module as a project dependency

Create index.js

$ touch index.js

Express can then be setup with a few lines of code:

// index.js
var express = require('express');
var app = express()
app.get('/', (request, response) => { response.send("Hello world!"); })


The express variable is set to the module 'express'. App is set to an instance of the express created on call. The app then can respond to get requests at the '/' end point (after the base URL), and sends the response of "Hello world!" to the request. Finally, the app listens on port 3000.

To run, simply enter $node index.js in the command line.

You can also setup 'scripts' in package.json to run
Add the line within the script: {} braces:

"start": "node index.js",

Then call $ npm start instead.

When running, the API response will be located at: http://localhost:3000

Now you have a sample Express API!

I'm beginning to write a few more applications with JavaScript/TypeScript than before; as well as other apps that are culminating to something outside of just knowledge I hope. This is a log of that practice, work and the journy. I look forward to making more projects and posts. I will post more work on my github as time keeps flying during quaruntine, so give me a follow if you'd like to keep up to date there as well.

Thanks for taking the time to read this and get to know me a little bit more. Looking forward to the road ahead.

All the best - Spencer

Spencer Pollock

Written by Spencer Pollock who lives in Vancover, BC, Canada. Computer developer. Working to make things simpler. I'll tell you my story as it unfolds. For more information, check out the main site here.