Node gets the URL address
1 varHTTP = require ("http");2 varurl = require (' URL ');//node. js comes with the URL3 varRouter = require ('./router '));4 5Http.createserver (function(request,response) {6Response.writehead (200,{' content-type ': ' Text/html;charset=utf-8 '});7 //first without the IF, the printed result has been favicon.ico, resulting in the following error, plus this judgment OK8 if(request.url!== '/favicon.ico '){9 varpathname = Url.parse (request.url). Pathname;//path name with '/'TenPathname=pathname.replace (/\//, ");//Replace '/' One Console.log (pathname); A //calling methods in router - Router[pathname] (request,response); -Response.End (' '); the } -}). Listen (8000); - -Console.log ("Server at Running http://127.0.0.1:8000");
Router.js
1 module.exports={2 Login:function(req,res) {3 Res.write ("I am the login method"); 4 },5 Register:function(req,res) {6 res.write (" I am The Register method "); 7 }8 }
In the browser input: Localhost:8000/login print out "I am the login method"
Localhost:8000/register print out "I am the Register method"
Node Learning day three (2)