Series: Introduction to the MEAN Stack

Connect is a node framework http that builds on the previously introduced module and is the basis for Express . In this article, I show you how to program a static file server with Connect. I also tell you what a middleware is.

Install Connect with the following command into a new project folder:

1
$ npm install connect

In the current folder should be a folder named node_modules , which in turn contains a folder named connect . At the time of writing, the latest version of Connect is 2.9.0.

In a new file server.js, which node_modules should be created in the same folder as it is, the Connect module can now be required .

1
var connect = require ‘connect’ ;

Note that it require does not have to be in again  node_modules . The requirefunction was developed so that it always first  searches the folder node_modules for a module with the name connect. If a module is not found with these names, require the path of the current file ( server.js) will run upwards until a node_modules folder with this module is found in a higher-level path . Finally, the globally installed modules are searched. If no module is found with this name, an error is output on the console. This would look like this:

1
2
3
4
5
6
7

module.js: 340 

throw err;

^

Error: Can not find module ‘connect’

With Connect you can very quickly develop a static file server. You only need four lines of code:

1
2
3
4
var connect = require ‘connect’ 
connect )
use ( connect. static ( __dirname )
listen 1337 ‘127.0.0.1’ ;

The Connect module is essentially a constructor function for creating a server – similar to the function before . It even contains an identically constructed function, which takes the same arguments. Only the function  is new. This function accepts so-called middleware as parameters.http.createServerlistenuse

Middleware is a function that accepts requests and processes them in responses. As far as nothing new compared to a normal server. The special feature of middleware is that several middleware functions can be connected one after the other. The middleware functions are then retrieved in a sequential order. However, a middleware can also prevent subsequent middleware from being called. Middleware encapsulates certain logics – such as the logic of a static file server – and makes them reusable.

Connect is a middleware framework. It allows the middleware to be routed use and provides a number of its own middleware functions, such as  for a static file server. Middleware can receive parameters for further configuration. When  a parameter can be the file path to a folder whose files are to be made available via the server. In this case, we pass a variable created by Node, which contains the path to the folder of the current file (that is ).connect.staticconnect.static__dirnameserver.js

The static file server can be started via node:

1
$ node server.js

If you call http://127.0.0.1:1337/ in the browser, then you get the message “Can not GET /”. This is an error message from Connect, which means that there is no file in the “/” path – which is also true. This path is a folder and not a file. If you call the address http://127.0.0.1:1337/server.js instead , the browser displays the source code server.js . So it returns the file to the browser – the server works!

The static file server works!

At this point, any HTML, CSS and JavaScript files could be created and packed into our project folder. The server outputs each file correctly, and if a project does not need dynamic data from a database or other functions, it is sufficient to create simple web projects.

The complete overview of each middleware that Connect provides is available at http://www.senchalabs.org/connect/ . Connect is mainly developed by TJ Holowaychuk.

In the next article I explain to you the Framework Express, which extends the functions of Connect.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: