Node js Web application Development Part 2

nodemongo

Here is the first part of this post. This post will demonstrate on how to connect your node js application into mongodb database.

Database :

mongoexpress1

Content :


{
 _id: ObjectId("52bbc6a47e197b1e8d00004a"),
 username: "testuser1",
 email: "testuser1@testdomain.com"
}

Here is the modified app.js file

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

// New Code
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('mongodb://<username>:<password>@paulo.mongohq.com:10049/nodeexpress1');  // remote mongodb databse

//var db = monk('localhost:27017/nodeexpress1');   this is for the local db connections.

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
 app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);
app.get('/helloworld',routes.helloworld);
app.get('/userlist', routes.userlist(db));    // this will load the db data to html

http.createServer(app).listen(app.get('port'), function(){
 console.log('Express server listening on port ' + app.get('port'));
});

Now create userlist.jade as follows.


extends layout
block content
 h1.
 User List
 ul
 each user, i in userlist
 li
 a(href="mailto:#{user.email}")= user.username

This will display the username with the attached link with email address.

Now run the app using node app.js command

Go to http://localhost:3000/userlist link. Here is the result

mongoexpress2

 

And this is how jade works.

example.jade


doctype html
html(lang="en")
 head
 title= pageTitle
 script(type='text/javascript').
 if (foo) {
 bar(1 + 5)
 }
 body
 h1 Jade - node template engine
 #container.col
 if youAreUsingJade
 p You are amazing
 else
 p Get on it!
 p.
 Jade is a terse and simple
 templating language with a
 strong focus on performance
 and powerful features.

equivalent html file

example.html


<!DOCTYPE html>
<html lang="en">
 <head>
 <title>Jade</title>
 <script type="text/javascript">
 if (foo) {
 bar(1 + 5)
 }
 </script>
 </head>
 <body>
 <h1>Jade - node template engine</h1>
 <div id="container" class="col">
 <p>You are amazing</p>
 <p>
 Jade is a terse and simple
 templating language with a
 strong focus on performance
 and powerful features.
 </p>
 </div>
 </body>
</html>

cheers 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s