Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced Express Web Application Development

You're reading from   Advanced Express Web Application Development For experienced JavaScript developers this book is all you need to build highly scalable, robust applications using Express. It takes you step by step through the development of a single page application so you learn empirically.

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781783282494
Length 148 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrew Keig Andrew Keig
Author Profile Icon Andrew Keig
Andrew Keig
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Advanced Express Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Foundations 2. Building a Web API FREE CHAPTER 3. Templating 4. Real-time Communication 5. Security 6. Scaling 7. Production Index

Feature: Heartbeat


As an administrator
I want to visit an endpoint
So that I can confirm the server is responding

Let's add a test to ./test/heartbeat.js for our Heartbeat feature. This resource will get a status from the route /heartbeat and return a 200 Ok status code. Let's write our first integration test using Mocha and SuperTest. First off, create a folder named /test inside your vision folder.

Our test describes heartbeat; it expects the response to have a JSON content type and a status code equal to 200 Ok.

var app = require('../app')
, request = require('supertest');

describe('vision heartbeat api', function(){
  describe('when requesting resource /heartbeat', function(){
    it('should respond with 200', function(done){
      request(app)
      .get('/heartbeat')
      .expect('Content-Type', /json/)
      .expect(200, done);
    });
  });
});

Let's implement the Heartbeat feature; we start by creating a simple Express server, ./lib/express/index.js. We include the express and http modules and create an Express application. We then add an application setting via app.set called port and set it to 3000. We define a /heartbeat route via app.get with which we pass a request handler, function, that takes two parameters: req (request) and res (response). We use the response object to return a JSON response. We create an HTTP server with http.createServer by passing our Express application to it; we listen on port 3000 as defined in our application setting called port. We then export the application with module.exports; exporting the application allows us to test it.

var express = require('express')
  , http = require('http')
  , app = express();

app.set('port', 3000);

app.get('/heartbeat', function(req, res){
  res.json(200, 'OK')
});

http.createServer(app).listen(app.get('port'));
module.exports = app;

We now create ./app.js in the root of our project and export the express module:

module.exports = require('./lib/express');

To run our test, execute the following command:

mocha

You should then receive the response:

1 tests complete (14 ms)

If successful, try running the application by executing this command:

npm start

With the app running, run the following curl command in a new terminal and you can see our heartbeat JSON response return a 200 Ok status code:

curl -i http://127.0.0.1:3000/heartbeat

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 4
Date: Fri, 14 Jun 2013 08:28:50 GMT
Connection: keep-alive
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime