Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

Reader rating:
Click here to read 1 comment about this article.
Article source: CoDe (2012 Sep/Oct)


Article Pages:  1  2 3 4 5 - Next >


Building Web APIs with Node.js and MongoDB

Node.js by itself is a blank canvas. However, the Express.js web framework for Node.js provides programmers with a clear, concise development platform to build high performance Web APIs.

In this article, I’ll illustrate what it takes to build a simple Web API using Node.js and the Express.js web framework with MongoDB as the persistence mechanism. Under the hood I’ll use MongoDB and Mongoose to persist the data.

NOTE: You can find the complete source code for this article at: https://github.com/donnfelker/workout-tracker

The Node.js product is still in its infancy yet it’s already being touted in many tech circles as the quickest way to develop scalable APIs (and applications) that can handle a large load directly out of the box. The main benefit of Node.js is that any existing JavaScript knowledge you have instantly transfers over to Node.js as it is a JavaScript framework. While this is a net gain, the unfortunate consequence of a new framework like Node.js is that you’re left with a blank canvas to start your development from. Building a Web API from the ground floor is not only time consuming but it is also very error prone in the long run. Thankfully, Express, the high performance web framework for Node.js, exists. Express allows you to quickly create applications that have a majority of the common boilerplate code already written for you. In this article, you’ll learn how to use the Express web framework for Node.js by building a simple Web API for a sample application known as the Workout Tracker.

Persistence in any Web API is the cornerstone of development. A common problem in API development is that the inserts or reads are far too slow due to the normalization of the data in a typical relational database. While there are ways to improve these queries (and insertions) with proper tuning of the database, one alternative up and coming approach is to use MongoDB as the backing store for APIs that require high throughput. Modeling the data in a document-oriented fashion (as MongoDB does) allows the developer to tackle the issue from a different angle - allowing the developer to store the data for the model in a complete entity in a MongoDB document. I’m not advocating that one approach is better than another, only that options from one approach might make more sense than another. In this article, I will show you how you can integrate MongoDB with Node.js to create a simple Web API using the Mongoose package.

Standing on the Shoulders of Giants

Node.js has not been around long - a mere three years - not even hitting an official version 1.0 (for this article I’ll use Node v0.8.0). During the last three years, various companies have started to adopt Node.js as part of their core business strategy. One of the alluring aspects of Node.js is the ability to share JavaScript between the client and the server.

While many web developers already know JavaScript and want to learn Node.js, some are turned off at the thought of working with Node.js because there is not a web framework built into the core of Node.js. I was no exception. Upon my first glance at version v0.2.x of Node.js, I thought the feature set was somewhat bare and the landscape of available packages (and documentation and examples) was sparse to say the least. It was frustrating to see that I might have to spend a large amount of time building core components simply to be productive in this new environment. This was also of major concern to a lot of other developers (and sometimes still is). Most developers are concerned with how productive they will be. If you want a solution that makes working in Node.js easier, you’ll want to look at one of the few various web frameworks for Node.js. Some of those include Express, Flatiron and Geddy. In this article I will work with Express because Express is the most popular Node.js web framework at the moment.

Creating Your First Express App

In order to develop an Express application you must install Express first. To install Express, you need to have Node installed (I’m using v0.8.0 - get Node.js at nodejs.org). To install Express from the command prompt, type:

npm install -g express

This command instructs NPM (Node Package Manager) to install Express. After a moment of some values streaming across the screen you will have installed Express! Using the -g switch instructs NPM to install the package globally inside of your current Node.js installation - meaning that you can access Express anywhere, not just in your current project.

To create your first Express app, navigate to the destination where you’d like your application files to reside (e.g., /development or C:\development if you’re on Windows). Next, issue the following command from the command prompt:

express ./workout-tracker

This will execute the express command that will create the Express application inside of a folder called workout-tracker. I will build this application during the course of this article. The Workout Tracker Express application is an API that will allow a user to keep track of the workouts they have performed through API commands.

Once the command above finishes you will need to change directories and install the dependencies that the application needs.

cd ./workout-tracker
npm install -d

The -d switch on the npm install command informs npm to provide an information-level log output during the installation process of the dependencies that npm is installing.

How does NPM know what the dependencies are? If no argument is supplied to the npm install command then npm will look for a file called package.json in the current directory and will read that file for all of the dependencies that need to be installed. Express created the package.json file during the express application generation process. The package.json file is very similar to the Gemfile featured in Rubygems in the Ruby language. The npm install command walks the package.json file and installs each of the dependencies.

&

By: Donn Felker

Donn Felker is the Principal for Agilevent, a Microsoft Partner he founded in Minneapolis, Minnesota. He is an experienced Software Architect/Developer with over nine years of professional experience in various markets that include: entertainment, health, retail, insurance, financial, and real estate.

He is a Microsoft ASP Insider who currently holds certifications as an MCTS in Web Client Development for the ASP.NET 3.5 Framework, Certified Scrum Master and ITIL Foundation v2. He also holds a bachelors of science in Software Engineering. He writes, presents and consults on various topics ranging from architecture to agile practices to patterns and practices. He is the founder/coordinator of the Twin Cities Give Camp, President of the Twin Cities Developers Guild and the founder of Twin Cities Pragmatic Beer.

You can read Donn’s blog at http://blog.donnfelker.com

dfelker@gmail.com

Fast Facts

Node.js and MongoDB both use JavaScript as the core language. If you build your front-end HTML client with tools like Backbone or Angular you will have a full stack application that shares its core language of JavaScript.



Article Pages:  1  2 3 4 5 - Next Page: 'Exploring the Express Application Assets' >>

Page 1: Building Web APIs with Node.js and MongoDB
Page 2: Exploring the Express Application Assets
Page 3: Writing Your First Route
Page 4: Integrating Express and Mongoose

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4.6 out of 5

7 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Sharepoint TechCon

 

SSWUG