node.js 路由详解

路由的基本使用

第一步:获取url跟目录下的字符

var http = require('http');
var url = require('url')

http.createServer(function (request,response) {
    response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
    if(request.url !== "/favicon.ico"){

        //拿到浏览器访问的url路劲,并且替换掉前面的/
        var pathname = url.parse(request.url).pathname.replace(/\//, '')
        console.log(pathname)
        
        response.end("")
    }
}).listen(9000)

浏览器访问http://localhost:9000/login

后台拿到访问路劲login

第二步:路由功能的实现

路由结合读取文件

猜你喜欢

转载自www.cnblogs.com/LO-ME/p/10886810.html