Preliminary routing in node.js

1. Create n4_root.js

var     http = require('http' );
 var     url = require('url'); // This is the one that comes with node.js var router = require('./router'); 
http.createServer( function     (request , response) {
        response.writeHead( 200, {'Content-Type': 'text/html; charset=utf-8' });
         if (request.url!=="/favicon.ico" ){
                 var     pathname =     url.parse( request.url).pathname;
                 // request.url gets the url in the input box 
                // console.log(pathname); 
                pathname = pathname.replace(/\//, ''); // replace the previous one 
                // console.log(pathname); 
router                 [pathname](request,response);
                response.end('');
        }
}).listen(8888);
console.log('Server    running    at    http://127.0.0.1:8888/');   
By var pathname =     url.parse(request.url).pathname; is the path to the root directory   http://127.0.0.1:8888 (root directory) is a / 


by pathname = pathname.replace(/\//, '' ); // Replace the previous / and enter http://127.0.0.1:8888/login, it will show that the login

can be done after getting the login.
Create a new router.js
module.exports={
    login:function(req,res){
        res.write( "I am the login method" );
    },
    zhuce:function(req,res){
        res.write( "I am the registration method" );
    }
}
 

The result after calling is like this

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324582944&siteId=291194637