I will directly start doing things instead of typing a great story why we needed it and what for.

Ergo, we needed an API. Done.

 

Lets start, open up your console and create a new folder called API

mkdir api
cd api

If you want to, put it in a GIT repo, thats your choice.

Install NodeJs if you havent already:

brew install nodejs

// OR
apt-get install nodejs

Create a new file called package.json and put in the following code:

{
    "name": "nodejs-api",
    "main": "serve.js"
}

Save it and move back to your normal console. Now we want Express to be a dependency. We could have added it in the package.json but we want the latest version so we let NPM fix this for us. Also we want body-parser as it can get the contents of POST requests.

Execute:

npm install express --save
npm install body-parser

Allright, lets get the actual coding, create a new file called serve.js. If you want to change the name, be my guest, but then change it in the package.json also.

First of all, lets start with the very basics of our script. I used port 80, you might want to change that later.

// Get all the packages that are needed for the API
var express    = require('express');        
var bodyParser = require('body-parser');

// Set the port we will be using for the API
var port = process.env.PORT || 80 

// Define the API object.
var api = express();

Next we want to configure our API to use the body-parser parts we need, this will give us the possibility to read incoming data.

// Configure what our API should use from the bodyParser package
api.use(bodyParser.urlencoded({
	extended:true
}));

api.use(bodyParser.json());

Next up, the routing / router. First we can do this in our server file, we might want to consider changing that to a dedicated file for it. Of course we need to route the requests to the right spot, so here we go:

// The routing part of our API, if extending the API it should be in it's own routing.js file.

var router = express.Router();

// A route to check if everything is working like it should
router.get('/', function(req, res) {
 res.json({ message: 'Take a beer or 2, you just found me.' }); 
});

// Set all routes being prefixed by api.
api.use('/api', router);

Almost there, last part before we can test this thing.

// And now, let the fun start; or our server of course.
api.listen(port); 

// And log it away
console.log('Well, looks like you found Lucky Luke. ' + port);

Lets fire ‘er up.

node serve.js

You can test either trough a browser or with Postman or any other tool you know that can send data over the line. I’m just using my own browser build by my bitch, Google Inc. In any case go to http://localhost/api

If everything went right, you should get a message;

Try to go to another URL, for instance http://localhost/api/somethingElse as you can see, Node is not serving it.

 

In fact, we have our server spinning and working already. I will show in the next tutorials how you can make use of a datamodel with MySql, using a dedicated routes file, making use of multiple models, authentication and much more. Stay tuned! Find the next one here.

 

The complete code is on Github in its own Branch