Working with routes in Express
Building routes for our backend application in Express is key to separating the static content that will be served for our Angular application from our API endpoints, which will respond with JSON data. Routes in Express are actually just layers of middleware defined to work on specific routes of our web server.
Getting ready
Let's create a simple status page for our future API to check whether it's working. We will make two different endpoints; one that simply returns the API is running
text, and one that will return a JSON representation of our route parameters merged with any query parameters.
How to do it...
Let's perform the following steps to create an API status endpoint for our Express application:
- First, let's create a new route for all our API requests. Create a new file called
/routes/api.js
; inside this file, we will define two new routes:
var express = require('express'); var router = express.Router(); router.get('/', function(req, res, next) { res...