nodejs路由初步

版权声明:版权有就有吧。 https://blog.csdn.net/m0_38044453/article/details/83543687

首先拿到请求的url,然后解析,把根目录下的字符串拿到。通过字符串访问对应的方法。在方法里面读取对应的html,然后展示到界面上。

路由的js文件

module.exports={
	login:function(req,res){
		res.write('我是login方法');
	},
	zhuce:function(req,res){
		res.write('我是注册方法');
	}
}

调用的js

var http = require('http');

var url = require('url');

var routmodel = require('./routmodel');

http.createServer(function (request,response){
	response.writeHead(200, {'Contet-Type':'text/html;charset=utf-8'});
	
	if(request.url!=='/favicon.ico'){
		var pathname = url.parse(request.url).pathname;
		pathname = pathname.replace(/\//,'');// 替换掉前面的/
		
		routmodel[pathname](request,response);
		response.end('');

	}	
	
}).listen(8000);

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

猜你喜欢

转载自blog.csdn.net/m0_38044453/article/details/83543687