Use routing methods in node.js

1. The find method of the array still doesn’t work, so change to filter

2. The regular expression is still not well understood

//var myrouter = require("./myrouter") ;

//
myrouter.getRouteValue(‘login’)(1,2);

pathname
= ‘/login‘
pathname
= pathname.replace(///, ""); //Replace the previous /
console.log(‘/‘); //output/
console.log('/'); ////output/
console.log(pathname);

// Grammar: According to the one on both sides // as the pattern definition/ pattern/attributes
//
Analysis: The // on both sides is the pattern delimiter; the middle / means /, which is the /< in /login /span>

var myrouter_action = [];

myrouter_action.push({ xpath:
'/login', yvalue: function(req, res) {
res.write(
"I am the login method");
console.log(
101);
}});

myrouter_action.push({ xpath:
'/register', yvalue: function(req, res) {
res.write(
"I am the registration method");
console.log(
102);
}});

myrouter_action.push({ xpath:
'/logout', yvalue: function(req, res) {
res.write(
"I am a logout method");
console.log(
103);
}});


myrouter_action.push({ xpath:
'/', yvalue: function (req, res) {
res.write(
"I am the root directory method");
console.log(
100);
}});


/* Find the function marked as opath from arr*/
function getRouteValue(opath) {
var filterArray = myrouter_action.filter(function(v) {
return v.xpath === opath
})
if (filterArray.length) {
return filterArray[0].yvalue
}
else{
console.log(
‘Failed to find routing table!’, opath);
}
}


module.exports
={
getRouteValue
}

var http = require("http");

var url = require("url");
var myrouter = require("./myrouter");

http
.createServer(
function(request, response) {
response.writeHead(
200, {"Content-Type": "text/html; charset=utf-8" });
if (request.url !== "/favicon.ico" ) {
console.log(
"1:",request.url);
var pathname = url.parse(request.url).pathname; //Get the requested path
console.log("2:",pathname);
//pathname = pathname.replace(///, " "); //Replace the previous/
//console.log("2",pathname);
//myrouter.getRouteValue(pathname)(request, response);
myrouter.getRouteValue(pathname)(request, response);

response.end(
"");
}
})
.listen(
8000);

console.log(
"Server running at http://127.0.0.1:8000/");


//console.log(myrouter.getRouteValue('login') (1,2));

//var myrouter = require("./myrouter");

//
myrouter.getRouteValue(‘login’)(1,2);

pathname
= ‘/login‘
pathname
= pathname.replace(///, ""); //Replace the previous /
console.log(‘/‘); //output/
console.log('/'); ////output/
console.log(pathname);

// Grammar: According to the one on both sides // as the pattern definition/ pattern/attributes
//
Analysis: The // on both sides is the pattern delimiter; the middle / means /, which is the /< in /login /span>

var myrouter_action = [] ;

myrouter_action.push({ xpath:
'/login', yvalue: function(req, res) {
res.write(
"I am the login method");
console.log(
101);
}});

myrouter_action.push({ xpath:
'/register', yvalue: function(req, res) {
res.write(
"I am the registration method");
console.log(
102);
}});

myrouter_action.push({ xpath:
'/logout', yvalue: function(req, res) {
res.write(
"I am a logout method");
console.log(
103);
}});


myrouter_action.push({ xpath:
'/', yvalue: function (req, res) {
res.write(
"I am the root directory method");
console.log(
100);
}});


/* Find the function marked as opath from arr*/
function getRouteValue(opath) {
var filterArray = myrouter_action.filter(function(v) {
return v.xpath === opath
})
if (filterArray.length) {
return filterArray[0].yvalue
}
else{
console.log(
‘Failed to find routing table!’, opath);
}
}


module.exports
={
getRouteValue
}

var http = require("http");

var url = require("url");
var myrouter = require("./myrouter");

http
.createServer(
function(request, response) {
response.writeHead(
200, {"Content-Type": "text/html; charset=utf-8" });
if (request.url !== "/favicon.ico" ) {
console.log(
"1:",request.url);
var pathname = url.parse(request.url).pathname; //Get the requested path
console.log("2:",pathname);
//pathname = pathname.replace(///, " "); //Replace the previous/
//console.log("2",pathname);
//myrouter.getRouteValue(pathname)(request, response);
myrouter.getRouteValue(pathname)(request, response);

response.end(
"");
}
})
.listen(
8000);

console.log(
"Server running at http://127.0.0.1:8000/");


//console.log(myrouter.getRouteValue('login') (1,2));

Leave a Comment

Your email address will not be published.