Nodejs:路由

router.js

//-----------------router.js--------------------------------
module.exports = {
	login: function (req, res) {
		res.write("我是login方法");
	},
	zhuce: function (req, res) {
		res.write("我是注册方法");
	}
}
//---------n4_rout.js路由-----------
var http = require('http');
var url = require('url');
var router = require('./model/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;
		console.log(pathname);
		pathname = pathname.replace(/\//, ''); //替换掉前面的/
		console.log(pathname);
		router[pathname](request, response);
		response.end('');
	}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

猜你喜欢

转载自blog.csdn.net/u013101178/article/details/84454629