DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > Node.js: Using Connect with Express

Node.js: Using Connect with Express

Chad Lung user avatar by
Chad Lung
·
Jan. 18, 12 · Performance Zone · Interview
Like (0)
Save
Tweet
6.53K Views

Join the DZone community and get the full member experience.

Join For Free

Connect is a middleware framework for Nodejs and is used in several projects like Express. Today I’ll show you a simple example of how to use some of the Connect bundled middleware in an Express application.

Assuming you have Nodejs installed and are ready to go, make sure to use npm to install the latest Express. I’ll install mine locally to the folder I’m currently working in like this:

$ npm install express

Lets use the Connect logger and responseTime middleware. The logger gives a configurable output and the responseTime will tell you how long a response took by placing a value in the headers (X-Response-Time).

Create a new file called app.js and add the following code:

var express = require('express');
 
var app = express.createServer(
    express.logger(),
    express.responseTime()
);
 
app.get('/', function(request, response) {
    response.send('Hello World');
});
 
console.log('Starting server...')
app.listen(8080);

You can see how easy it is to add Connect middleware. Using Express you simply add the middleware you want, in this case I added it in via createServer.

Now go to the URL (you might want to use a browser add-on to see the headers more easily, for Chrome try the XHR Poster). In the header you should see something similar to this:

X-Response-Time 0ms

Obviously our server is so fast at this point since it only serves up the text “Hello World” that it took essentially no time to process and response. However if you were actually doing some something more you will see the value go up. Just as an example you could temporarily block the server with a timer if you wanted to see the value change.

In the output you will see the logging:

127.0.0.1 - - [Sun, 15 Jan 2012 23:57:13 GMT]
"GET / HTTP/1.1" 200 11 "-"
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2)
AppleWebKit/535.7 (KHTML, like Gecko)
Chrome/16.0.912.75 Safari/535.7"

 

From http://www.giantflyingsaucer.com/blog/?p=3537

Express Node.js

Published at DZone with permission of Chad Lung, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • MACH Architecture Explained
  • Java’s Encapsulation - When the Getter and Setter Became Your Enemy
  • A Brief History of the DMCA
  • Refactor Switch to a One-Liner

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo