nodejs学习笔记之路由
程序员文章站
2022-04-10 18:31:44
server.js
var http = require("http");
var url = require("url");
function start(...
server.js
var http = require("http"); var url = require("url"); function start(route){//route.js 的route方法 //服务实现 function onrequest(request,response){ var pathname = url.parse(request.url.).pathname; route(pathname); // response.writehead(200,{"content-type":"text/plain"}); response.write("只是一个练手罢了"); response.end(); } //创建服务 http.createserver(onrequest).listen(8888);//端口号8888 console.log("服务启动");//后台输出,表示服务启动成功 } exports.start = start; //将start方法暴露出去
========================================
route.js
function route(pathname){ console.log("请求路径是:" + pathname); //后台查看请求路劲 区分请求的地址 } exports.route = route;
========================================
index.js
var server = require("./server"); var router = require("./route"); server.start(router.route);//传递給start的是router的route方法