nodejs the http server, how to use the GET, POST requests to send data

Pass parameters to the server browser, the address bar is the most commonly used parameter passing (get), and form submission (post)

Let me get mass participation, it is to keep up in the url? Key = value & key2 = value2 ......

However, according to several http server before, when we visit localhost:? 3000 / text a = 1, but the server returns a 404 page

The terminal is printed request.url this: / text a = 1?

This of course can not match the routing method, of course, want to return to page 404

First of all we just want to get to? Previous path number, go find a way to get to? After a number of parameters, so it does not return a 404 page

nodejs has a special deal with the url core library, you can take various values ​​on the address bar, click below to change before the http server code, to get to the url and path parameters:

The test over the entire server code:

api.js :( no change)

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

var handle = {};

handle["/"]=handler.home;
handle["/home"]=handler.home;
handle["/review"]=handler.review;
handle["/api_records"]=handler.api_records;
handle["/text"]=handler.text;

server.startServer(router.route,handle);

server.js :( changes are subject to change marked with red font)

var HTTP = the require ( "HTTP" );
 var = the require URL ( "URL"); 

var the startServer = function (route, handle) {
     var onRequest = function (Request, Response) {
         var pathname = url.parse (request.url ) .pathname; // get to? Url path zenkakugo 
        the console.log ( "Request Received" + pathname);
         var url.parse the params = (request.url, to true) .query ; // get to the url? After the contents of the second parameter is a Boolean value, true: Format false: unformatted returns the string of 
        route (handle, pathname, Response, the params ); 
        
    } 

    var Server = http.createServer (onRequest);
     / /Finally, let the server listens on a port 
    server.listen (3000, "127.0.0.1"); // You can also add a second parameter 127.0.0.1 represents the local 

    console.log ( "Server Started ON localhost Port 3000"); / / plus server starts up a prompt 
} 

module.exports.startServer = the startServer;

router.js (subject to change)

var fs = require("fs");

function route(handle,pathname,response,params){
    console.log("Routing request for"+pathname);
    if(typeof handle[pathname] === 'function'){
        handle[pathname](response,params);
    }else{
        response.writeHead(200,{"Content-Type":"text/html"});
        fs.createReadStream(__dirname+"/404.html","utf8").pipe(response);
    }
}

module.exports.route=route;

handler.js (subject to change)

var fs = require("fs");

function home(response,params){
    response.writeHead(200,{"Content-Type":"text/html"});
    fs.createReadStream(__dirname+"/index.html","utf8").pipe(response);
}

function review(response,params){
    response.writeHead(200,{"Content-Type":"text/html"});
    fs.createReadStream(__dirname+"/review.html","utf8").pipe(response);
}

function api_records(response,params){
    response.writeHead(200,{"Content-Type":"application/json"});
    response.end(JSON.stringify(params));
}

function text(response,params){
    response.writeHead(200,{"Content-Type":"text/plain;charset=UTF-8"});
    response.end("文本内容");
}

module.exports={
    home,
    review,
    api_records,
    text
}

The above code is about to deal with all the changes get requests, we api_records method handler.js in the middle, to take back params returns to the page, enter: localhost: 3000 / api_records a = 1 & b = 2?

Page will exhibit {a: 1, b: 2}

After this, we get to the parameter values ​​on the server side, you can go back to the page content corresponding to the parameter values! ! !

 

Guess you like

Origin www.cnblogs.com/fqh123/p/11258681.html