fbpx

Series: Introduction to the MEAN Stack

Today I present to you the Framework Express , which is the most watched module at npm. I also explain to you the concept of a route.

Express relies on Connect and is also developed by TJ Holowaychuk. It expands Connect with additional middleware and an API for routing . In this case, the term routing is used to assign URLs (and HTTP methods) to a middleware. Thus, different areas of a web page can use different middleware. If a certain area of a website is to be password-protected, for example, a corresponding authentication medium can be used explicitly only for this area.

The previous examples of the HTTP and file server are now combined with Express. In a new project folder you must first install Express:

1
$ npm install express

At the time of writing is the latest version of Express 3.4.0. Here is the code for the server in a new file server.js:

1
2
3
4
5
6
7
var express = require ‘express’ 
express )
get ‘/’ function ( req , res {
res. send ‘Hello World’ 
)
use ( express. static ( __dirname )
listen 1337 ‘127.0.0.1’ ;

The server code is almost identical to the example with Connect. Express takes any middleware from Connect, so it  can be used  instead . New is the following section:express.staticconnect.static

1
2
3
  , get ‘/’ function ( req , res {
res. send ‘HelloWorld’ 
)

This section reminds you of the first code example with the http module. Only the function get is new. It is similar usebut takes a route as the first parameter in addition to the middleware as the second parameter. In this case, the route is the URL of the root: /. However, the route could be any string other than a URL or a regular expression. This get is the corresponding HTTP method, so this route and these middleware will only be performed on a GETrequest for the URL / . Similarly, there is for each HTTP method a corresponding function from Express to set specific routes and middleware for this: , , etc.express.postexpress.delete

If you enter http://127.0.0.1:1337/ into the browser, you do not get the error message “Can not GET /”, but a “Hello World”. (Note: If you GETcall a URL through the browser, a query is always performed by default .) However , you can get the file via http://127.0.0.1:1337/server.jsserver.js .

But what if a route already exists for the URL http://127.0.0.1:1337/server.js ? We can easily test this with a slightly modified example:

1
2
3
4
5
6
7
8
9
10
var express = require ‘express’ 
express )
get ‘/’ function ( req , res {
res. send ‘Hello World’ 
)
get ‘/server.js’ function ( req , res {
res. send ‘This should be the server.js!’ 
)
use ( express. static ( __dirname )
listen 1337 ‘127.0.0.1’ ;

If you now call http://127.0.0.1:1337/server.js , you get the message “This should be the server.js!” Instead of the contents of the file server.js. This is the order in which the middleware was set! Our own middleware for the route /server.js was defined before the static file server and is therefore first executed. It depends on the middleware whether subsequent middleware is still called or not. In this case, we terminate with the method  in our own middleware to call up additional middleware. In this way, one could, for example, program an authentication medium that would block unauthenticated users and allow authenticated users to pass through static file servers.res.send

An example of a real and complex website, which is based on Express, is MySpace .

These basics are sufficient for the first walking tests in Node. In the next article, I will first explain to you MongoDB.


0 Comments

Leave a Reply

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