File search in Node.js

by Paul Vorbach, 2012-07-12

Since the German blog post Volltextsuche is the most clicked on this blog, I’ll explain, how I realised the full-text search of this website with the help of Node.js.

Prerequisites

You need to be on a Linux/Unix machine, since the search uses native command line commands like find and grep and you need to have Node.js (v0.4.0+) installed. Maybe there is a similar way to do it on Windows, too. I think you could install Git for Windows and add the /bin directory to your system PATH to use the same commands on Windows. If somebody has a better solution (maybe one that works out of the box) he/she may leave a comment.

Finding files on the command line

If you are in a Unix shell, you don’t have a nifty search box, that lets you search for files in your current working directory. Instead, you have the command find, that lets you search for files by specifying various arguments.

For example with find . -name '*.txt' you can search for all files (or directories) ending in .txt in the current working directory and in any subdirectories.

But how would you search for the contents of a file?

This is quite a bit more difficult to find out. There’s another command, grep, that allows you to filter lines from stdin or from the contents of a given file.

Usually, Unix shells allow you to use the pipe character | to combine commands. This way, you can use find to first find all files of a specific type and the to filter out all the files that contain a specific search string, as follows:

The command xargs makes sure, that the results of find are used as arguments for grep, so that it reads the contents of each file. As a result, the command returns a line seperated list of HTML files, that contain the string “example”.

To improve results and prevent errors, the command can still be improved.

This way, it doesn’t matter if file names are upper- or lowercase and the text search is also case insensitive. Binary content is ignored and warnings will be suppressed.

The basic code

Let’s have a look at the required JavaScript code.

What you need to write is a simple HTTP server, that parses the query part of the URL from a HTTP GET request. The search module only takes one parameter, s, like in http://vorb.de/search.html?s=example. This website (vorb.de) uses a small REST framework of mine, api, which enables me to register modules for different URLs, but I’ll only show how to do it without any framework. This way you can adapt the solution to your framework of choice (express, flatiron, etc.).

You need to load several packages during start-up:

After that, define some other values:

Now you can write the server code:

You may download the source file. You can start it on the command line with node file-search.js. Then go to localhost:8080 try it. Of course you need to create some HTML files first, if you want to find something.

Here’s how the result will look like:

Screenshot of the search results page
Screenshot of the search results page