Click here to Skip to main content
15,880,503 members
Articles / Programming Languages / Javascript

A simple log server using express, nodejs, and mongodb

Rate me:
Please Sign up or sign in to vote.
3.75/5 (10 votes)
30 Mar 2012CPOL3 min read 38.5K   20   5
In this article we are going to review a piece of NodeJS code and learn how to use some of its common modules. This is a simple application which keeps record of logs including errors, warnings and information.

Introduction 

In this article we are going to review a piece of NodeJS code and learn how to use some of its common modules. This is a simple application which keeps record of logs including errors, warnings and information.

The most noticeable point about this log server is the way it stores log data, which uses a sort of document-oriented database.

Get the source

You can get the code from here: https://github.com/afshinm/nodejs-log-server

We’ll use Mongoose and Express modules in this application. You can simply run a small website or weblog using these two modules! I’ll be explaining each module at first and then we’ll take a look at the code.

Modules  

Mongoose: It’s one of the best existing libraries for using mongodb through nodejs. Mongo is an open source document-oriented NoSQL database system.

Read more at: http://blog.learnboost.com/blog/mongoose/.

Express: is a web framework for nodejs. Using Express, you can generate your website or application’s interfaces and define a dedicated route for each one of them (for instance, your weblog’s article list will be /list and details page for each article will be like /blog/id/1).

Read more at: http://expressjs.com/.

Let's start

Let’s take a look at the actual code. First, we should call the two modules in the application:

JavaScript
var app = require('express').createServer(),
mongoose = require('mongoose'),

To easily create a schema, we’ll need to store it’s constructor in the Schema variable:

JavaScript
Schema = mongoose.Schema, 

Afterward, we’ll create two arrays to store constant values of the application:
JavaScript
priority = ['low', 'normal', 'high', 'critical'],
logtype = ['information', 'warning' ,'error']; 

The first array is a list of possible priorities for logs and the second array is the log type. Now it’s time to connect to the database and create the respective schema:
JavaScript
mongoose.connect('mongodb://example.com/logs');

logItem = new Schema({
    priority  : Number,
    logtype   : Number,
    datetime  : Date,
    msg       : String
});

As you can see, we are connected to the logs database and created our desired schema. This schema contains five fields which are log priority, log type, log creation date, and log message in order of appearance. After that, in the next line, we’ll introduce our model to the database:
JavaScript
mongoose.model('logItem', logItem);

Our model name is logItem. In the next line you can see the definition of a route for the Express module, which tells the module to do some stuff when somebody enters the main root:
JavaScript
app.get('/', function(req, res){ 

The callback method has got two parameters, where the first one will be containing the request data and the second one is the response variable. Using these two variables, we can do some stuff on a specific request or response. The following command will print some text on the output:
JavaScript
res.send("Log saved on " + Date()); 

And then, on the application console:
JavaScript
console.log("Log saved on " + Date()); 

What is the console.log?

console.log is a simple method which prints some text on the application console (stdout). console.log accepts one parameter which is the desired text to be printed on the screen. console.log does something similar in JavaScript and Firebug. For example if we use console.log in JavaScript code on the client-side, we can see the printed text in the Firebug’s console.

The only reason of using console.log is to know what the application is doing. Just this!

In the next line we’ve defined a variable and fill it with request parameters:

JavaScript
var reqQuery = req.query;
This variable is an object which contains all requested parameters (e.g. http://example.com/?foo=1&boo=2). The next line will get the model from the database:
JavaScript
var logItem = mongoose.model('logItem');

We do that to be able to insert, delete and update database records. In the next two lines, using request parameters, we’ll define log type and priority:
JavaScript
var pr = priority.indexOf(reqQuery["priority"]);
var type = logtype.indexOf(reqQuery["type"]);

In this way, we are sure that the stored data is in the range we defined. And finally we create a new model object with the desired variables:
JavaScript
new logItem({    
    datetime: Date(),
    priority: (pr >= 0 ? pr : 0),
    logtype: (type >= 0 ? type : 0),
    msg: reqQuery["msg"]})
.save();

Calling save() method will save the object in the database. After all, we’ll run the application on a port (e.g. 3000):
JavaScript
app.listen(3000); 

Result

Now, after running the application, we can simply add a log to our database using the following url:
http://example.com:3000/?msg=logtext&priority=high&type=errortext&priority=high&type=error

Special thanks to my dear friend, Fardin Koochaki for translating this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO usabli.ca
Iran (Islamic Republic of) Iran (Islamic Republic of)
I am a web developer, the co-founder of usabli.ca.
I love programming and also new technologies on Web Developing such as HTML5 and I always split my time between programming and reading.

Comments and Discussions

 
QuestionThanks my friend Pin
mohammad zargarani30-Mar-12 7:01
mohammad zargarani30-Mar-12 7:01 
AnswerRe: Thanks my friend Pin
Afshin Mehrabani30-Mar-12 7:20
Afshin Mehrabani30-Mar-12 7:20 
AnswerRe: Thanks my friend Pin
Amir Mohammad Nasrollahi9-Aug-13 20:55
professionalAmir Mohammad Nasrollahi9-Aug-13 20:55 
GeneralGreat article. Please keep it up! Pin
M.Soghandi30-Mar-12 6:12
M.Soghandi30-Mar-12 6:12 
GeneralRe: Great article. Please keep it up! Pin
Afshin Mehrabani30-Mar-12 6:56
Afshin Mehrabani30-Mar-12 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.