Creating a Basic API server

Let's create a super basic Node.js server using Express that we'll use to create our own API. Then, we can send tests to the API using Postman REST Client to see how it all works. In a new project workspace, first install the npm modules that we're going to need in order to get our server up and running:

$ npm init
$ npm install --save express body-parser underscore

Now that the package.json file for this project has been initialized and the modules installed, let's create a basic server file to bootstrap up an Express server. Create a file named server.js and insert the following block of code:

var express = require('express'),
    bodyParser = require('body-parser'),
    _ = require('underscore'),
    json = require('./movies.json'),
    app = express();

app.set('port', process.env.PORT || 3500);

app.use(bodyParser.urlencoded());
app.use(bodyParser.json());

var router = new express.Router();
// TO DO: Setup endpoints ...
app.use('/', router);

var server = app.listen(app.get('port'), function() {
    console.log('Server up: http://localhost:' + app.get('port'));
});

Most of this should look familiar to you. In the server.js file, we are requiring the express, body-parser, and underscore modules. We're also requiring a file named movies.json, which we'll create next.

After our modules are required, we set up the standard configuration for an Express server with the minimum amount of configuration needed to support an API server. Notice that we didn't set up Handlebars as a view-rendering engine because we aren't going to be rendering any HTML with this server, just pure JSON responses.

Creating sample JSON data

Let's create the sample movies.json file that will act as our temporary data store (even though the API we build for the purposes of demonstration won't actually persist data beyond the app's life cycle):

[{
    "Id": "1",
    "Title": "Aliens",
    "Director": "James Cameron",
    "Year": "1986",
    "Rating": "8.5"
},
{
    "Id": "2",
    "Title": "Big Trouble in Little China",
    "Director": "John Carpenter",
    "Year": "1986",
    "Rating": "7.3"
},
{
    "Id": "3",
    "Title": "Killer Klowns from Outer Space",
    "Director": "Stephen Chiodo",
    "Year": "1988",
    "Rating": "6.0"
},
{
    "Id": "4",
    "Title": "Heat",
    "Director": "Michael Mann",
    "Year": "1995",
    "Rating": "8.3"
},
{
    "Id": "5",
    "Title": "The Raid: Redemption",
    "Director": "Gareth Evans",
    "Year": "2011",
    "Rating": "7.6"
}]

This is just a really simple JSON list of a few of my favorite movies. Feel free to populate it with whatever you like. Boot up the server to make sure you aren't getting any errors (note we haven't set up any routes yet, so it won't actually do anything if you tried to load it via a browser):

$ node server.js
Server up: http://localhost:3500
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.161