Node:路由代码的实现

路由是指客户端请求地址与服务器端程序代码的对应关系,简单的说,就是客户端请求什么服务器端响应什么。
核心代码示例:

// 1.引入系统模块http
const http = require('http');
const url = require('url');

// 2.创建网站服务器
const app = http.createServer();
// 3.为网站服务器对象添加请求事件
app.on('request',(req,res) => {
    // 4. 实现路由功能
    // 1) 获取客户端的请求方式req.method  toLowerCase()将获取到的GET或POST请求方式转换成小写get或post
    const method = req.method.toLowerCase();
    // 2) 获取请求地址  req.url
    const urlpathname = url.parse(req.url).pathname;
    // 文本内容和编码处理
    res.writeHead(200,{
        'content-type':'text/html;charset=utf8'
    })
    // 判断请求方式 get或post
    if(method == 'get'){
        // 判断请求地址
        if(urlpathname == '/' || urlpathname == '/index'){
            res.end('Welcome to Homepage欢迎来到首页');
        }else if(urlpathname == '/list'){
            res.end('Welcome to Listpage欢迎来到列表页');
        }else {
            res.end('Not Found Page抱歉,没有找到该页面');
        }
    }else if(method == 'post'){

    }
});
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功');

执行结果如下:
地址栏输入/或/index后提示:
在这里插入图片描述
地址栏输/list后提示:
在这里插入图片描述
地址栏输入其他信息后提示:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AODV(Ad Hoc On-Demand Distance Vector)是一种基于距离向量的自适应路由协议,常用于无线自组织网络中。 以下是一个简单的AODV路由协议实现代码,基于ns-3网络模拟器。 ``` #include "ns3/aodv-helper.h" #include "ns3/aodv-routing-protocol.h" #include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/log.h" #include "ns3/mobility-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/point-to-point-dumbbell.h" #include "ns3/wifi-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE("AodvExample"); int main(int argc, char *argv[]) { LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL); // Create nodes NodeContainer nodes; nodes.Create(5); // Create channels PointToPointHelper p2p; p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps")); p2p.SetChannelAttribute("Delay", StringValue("2ms")); // Create point-to-point links NetDeviceContainer devices; for (uint32_t i = 0; i < nodes.GetN() - 1; ++i) { for (uint32_t j = i + 1; j < nodes.GetN(); ++j) { devices.Add(p2p.Install(nodes.Get(i), nodes.Get(j))); } } // Install AODV AodvHelper aodv; InternetStackHelper internet; internet.SetRoutingHelper(aodv); internet.Install(nodes); // Set mobility MobilityHelper mobility; mobility.SetPositionAllocator( "ns3::GridPositionAllocator", "MinX", DoubleValue(0.0), "MinY", DoubleValue(0.0), "DeltaX", DoubleValue(10.0), "DeltaY", DoubleValue(10.0), "GridWidth", UintegerValue(3), "LayoutType", StringValue("RowFirst")); mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); mobility.Install(nodes); // Assign IP addresses Ipv4AddressHelper ip; ip.SetBase("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer interfaces = ip.Assign(devices); // Add static routes Ipv4GlobalRoutingHelper::PopulateRoutingTables(); // Print routing table for each node for (uint32_t i = 0; i < nodes.GetN(); ++i) { Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout); nodes.Get(i)->GetObject<AodvRoutingProtocol>()->PrintRoutingTable(routingStream); } Simulator::Run(); Simulator::Destroy(); return 0; } ``` 以上代码中,我们首先创建了5个节点和点对点通信信道。然后,我们安装了AODV路由协议,并将其分配给每个节点。接着,我们设置了节点的移动模型,分配了IP地址,并为每个节点添加了静态路由。最后,我们打印了每个节点的路由表,并运行仿真。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值