Minimal Express server setup for API development

Initialise npm with defaults.

Create your main index.js entrypoint.

Install express, body-parser, morgan and winston packages.

Make your index.js look like this.

This is probably one of the most light weight node.js configuration that I have ever written for building simple REST web services.

In my opinion, this is a good starting point as it makes minimal assumptions about what you might need, letting you add whatever you need minimally on top.

Handling file uploads in Express using multer

First of all, download and install multer.

Include the dependency.

Initialise it into a service. The command below will initialise it with default storage type (being DiskStorage) and upload destination being the uploads/ folder. Continue reading

What I’ve learnt about writing Expressjs applications

There’s a lot of ways you could write an express application. If you searched for Express app generator, you’ll probably find at least five in the first page of search results.

In my opinion, most of these are designed for hackathons – to get you up and running as fast as possible but also as crudely as possible. While they are good when you want to try something out quickly, most of that code will turn into a big pile of mess as your application grows.

I’ve been in this situation before. I get my application up and running as fast as possible and in order to get my first MVP done but then quickly realise that the auto generated code that was meant to accelerate my development is now slowing me down. To remedy this, this is now how I develop my node.js applications. Continue reading

Creating a RESTful API (Express series 3)

So in the last post we went over the project structure. This time, we’ll actually modify some of the code to create our own RESTful web service.

If you don’t know what a RESTful web service is, you should check out the wikipedia entry for it.

As discussed in the previous post, all of our routes are defined in files located in the routes folder which are then referenced and collated in the app.js file. So, to create our own RESTful API, we need to do something in this area.

Lets checkout the index.js file and see whats in it. Continue reading

Understanding the default project structure (Express series 2)

Now that we have a basic express web app created, lets go through the project structure.

package.json
In the last post we briefly went through the purpose of package.json. Now its time to go through it in bit more detail. As the extension .json indicates, data in this file is in json format. Yours should look something like this: Continue reading