Getting started with our application

Let's start developing our sample application with Node.js, which will read a log file and parse its information using a regular expression. We are going to create all the required code inside a JavaScript file, which we will name as regex.js. Before we start coding, we will perform a simple test. Add the following content inside the regex.js:

console.log('Hello, World!'),

Next, in the terminal application, execute the regex.js command node from the directory that the file was created in. The Hello, World! message should be displayed as follows:

Getting started with our application

The hello world application with Node.js is created and it works! We can now start coding our application.

Reading a file with Node.js

As the main goal of our application is to read a file, we need the file that the application is going to read! We will be using a sample Apache log file. There are many files on the Internet, but we will be using the log file that can be downloaded from http://fossies.org/linux/source-highlight/tests/access.log. Place the file in the same directory that the regex.js file was created.

Note

This sample Apache log file is also available within the source code bundle from this book.

To read a file with Node.js, we need to import the Node.js filesystem module. Remove the console.log message we placed inside the regex.js file and add the following line of code:

var fs = require('fs'),

Note

To learn more about the Node.js filesystem module, please read its documentation at http://nodejs.org/api/fs.html.

The next step is to open the file and read its content. We are going to use the following code to do this:

fs.readFile('access.log', function (err, data) {//#1

  if (err) throw err;//#2

  var text = data.toString();//#3

  var lines = text.split('
'),//#4

  lines.forEach(function(line) {//#5
    console.log(line);//#6
  });
});

According to the Node.js documentation, the readFile function (#1) can receive three arguments: the name of the file (access.log), certain options (that we are not using in this example), and the callback function that will be executed when the contents of the file are loaded in the memory.

Note

To learn more about the readLine function, please access http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback.

The callback function receives two arguments. The first one is the error. In case something goes wrong, an exception will be thrown (#2). The second argument is data, which contains the file contents. We are going to store a string with all the file contents in a variable named text (#3).

Each record of the log is then placed in a row of the file. So, we can go ahead and separate the file records and store it into an array (#4). We can now iterate the array that holds the log rows (#5) and perform an action in each line. In this case, we are simply outputting the content of each line in console (#6) for now. We will replace line #6 of the code with a different logic in the next section.

If we execute the regex.js command node, all the file content should be displayed as follows:

Reading a file with Node.js
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset