Latest web development tutorials

Node.js route

We want to provide the URL request routing and other GET and POST parameters required, then these need to route data to perform the corresponding code.

Therefore, we need to view the HTTP request, extract the URL and GET / POST parameter request. This function should be part of the route or the server (or even as a function of the module itself) is really worth exploring, but here it is tentatively scheduled for our HTTP server.

All the data we need are included in the request object, the object as onRequest () first argument to the callback function is passed. But in order to resolve these data, we need additional Node.JS modules, which are url and querystring module.

                   url.parse(string).query
                                           |
           url.parse(string).pathname      |
                       |                   |
                       |                   |
                     ------ -------------------
http://localhost:8888/start?foo=bar&hello=world
                                ---       -----
                                 |          |
                                 |          |
              querystring(string)["foo"]    |
                                            |
                         querystring(string)["hello"]

Of course, we can also use the querystring module to parse the body of the POST request parameters, we will demonstrate later.

Now we come to onRequest () function to add some logic to find the URL path browser requests:

var http = require("http");
var url = require("url");

function start() {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

Well, our application can now URL path requested to distinguish between different requests - which allowed us to use the route (not yet completed) to the request to the URL path is mapped to the reference handler.

We want to build applications, which means that a request from the / start and / upload may use different code to handle. Later we will see how to integrate these elements together.

Now we can write the route, and create a file named router.js, add the following:

function route(pathname) {
  console.log("About to route a request for " + pathname);
}

exports.route = route;

As you can see, this code did not do anything, but for now it should be. Add more logic in the past, let's look at how to integrate routing and servers.

We should be aware that there is a routing server and use them effectively. Of course, we can be hard-coded in this way is bound to a dependency on the server, but the experience of other programming languages ​​told us it would be a very painful thing, so we'll use dependency injection Add route relatively loosely module.

First, let's expand the server start () function, so that the routing function as an argument in the past, server.js file code is as follows

var http = require("http");
var url = require("url");

function start(route) {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(pathname);

    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

At the same time, we will expand accordingly index.js, so routing function can be injected into the server:

var server = require("./server");
var router = require("./router");

server.start(router.route);

Here, we pass the function still has not done anything.

If you now start the application (node ​​index.js, always remember the command line), and then request a URL, you will see the application corresponding to the output information, which indicates that our HTTP server is already using a routing module, and will request the path is passed to the route:

$ node index.js
Server has started.

Output has been removed over the relevant part of the more annoying /favicon.ico request.

Browser access http://127.0.0.1:8888/, output results are as follows: