Node js Web application Development Part 2

Supun Dharmarathne
technodyne
Published in
2 min readDec 26, 2013
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 :

[sourcecode language=”js”]

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

[/sourcecode]

Here is the modified app.js file

[sourcecode language=”js”]
/**
* 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’));
});

[/sourcecode]

Now create userlist.jade as follows.

[sourcecode language=”js”]

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

[/sourcecode]

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

[sourcecode language=”js”]

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.

[/sourcecode]

equivalent html file

example.html

[sourcecode language=”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>

[/sourcecode]

cheers :)

--

--