Book Image

Learning Single-page Web Application Development

Book Image

Learning Single-page Web Application Development

Overview of this book

Table of Contents (15 chapters)
Learning Single-page Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Defining the API routes


One of the most important aspects of our API are routes that we take to create, read, update, and delete our speakers.

Our routes are based on the HTTP verb used to access our API, as shown in the following examples:

  • To create record, use the POST verb

  • To read record, use the GET verb

  • To update record, use the PUT verb

  • To delete records, use the DELETE verb

So, our routes will be as follows:

Routes

Verb and Action

/api/speakers

GET retrieves speaker's records

/api/speakers/

POST inserts speakers' record

/api/speakers/:speaker_id

GET retrieves a single record

/api/speakers/:speaker_id

PUT updates a single record

/api/speakers/:speaker_id

DELETE deletes a single record

Configuring the API routes:

  1. Let's start defining the route and a common message for all requests:

    var Speaker     = require('./server/models/speaker');
    
    // Defining the Routes for our API
    
    // Start the Router
    var router = express.Router();
    
    // A simple middleware to use for all Routes and...