02 路由

作者: 亨锅锅 | 来源:发表于2018-10-18 19:08 被阅读0次

题记:编程没有捷径,你敲坏多少键盘,就有多大出息
敢问路在何方,路在脚下。。。

路由就是你访问不同的资源,得到不同的响应内容
1 创建contact.html文件,api文件夹,api文件夹下创建docs.html文件
来看下面的代码

// 通过 http 模块,创建本地服务器
var http = require('http');
var fs = require('fs');

// 创建服务器方法
var server = http.createServer(function (req,res) {

    if(req.url !== '/favicon.ico'){

        if(req.url === '/home' || req.url === '/'){
            res.writeHead(200,{"Content-type":"text/html"});
            fs.createReadStream(__dirname + '/index.html','utf8').pipe(res);
        }else if (req.url === '/contact'){
            res.writeHead(200,{"Content-type":"text/html"});
            fs.createReadStream(__dirname + '/contact.html','utf8').pipe(res);
        }else if(req.url === '/api/docs'){
            res.writeHead(200,{"Content-type":"text/html"});
            fs.createReadStream(__dirname + '/api/docs.html','utf8').pipe(res);
        }else if(req.url === '/getJson'){
            var json = [{"name":"heng","age":18},{"name":"bob","age":11}];
            res.writeHead(200,{"Content-type":"application/json"});
            res.end(JSON.stringify(json));
        }else{
            res.writeHead(200,{"Content-type":"text/plain"});
            res.end('404 not found');
        }
    }
});

// 服务对象 监听服务器地址以及端口号
server.listen(8888,"127.0.0.1");

console.log('server is running');

分别访问
http://127.0.0.1:8888
http://127.0.0.1:8888/home
http://127.0.0.1:8888/contact
http://127.0.0.1:8888/api/docs
http://127.0.0.1:8888/getJson
http://127.0.0.1:8888/xxx
看看得到什么结果?

相关文章

  • 07-Vue路由vue-router

    01-路由入门 02-路由嵌套

  • 2018-09-24

    01 02 v-cloak 03 路由 GIJZQ`C(M)8NFGZWRL5(1$Q.png 04 路由嵌套...

  • 一款可能是最容易使用的对页面、服务的路由框架。使用APT实现。

    Jet RouterKit https://github.com/gybin02/RouterKit 路由库;An...

  • 02 路由

    题记:编程没有捷径,你敲坏多少键盘,就有多大出息敢问路在何方,路在脚下。。。 路由就是你访问不同的资源,得到不同的...

  • Gin框架全套实战免费视频教程

    01.API的URL规则设计、带参数的路由 02.是否一定要用MVC模式、路由分组 03.简单Dao层代码封装、使...

  • Camel(02) - 路由

    Camel路由简介 Camel最重要的特色之一就是路由,没有它Camel本质上只是一个传输连接器库。本篇文章将从各...

  • iOS组件化开发index

    01 iOS 组件化 —— 路由设计思路分析--冰霜02 蘑菇街 App 的组件化之路--Limboy03 蘑菇街...

  • Cx_BGP

    文档RFC4271RFC2918 Content Xx选路原则Xx聚合路由 iXx选路原则[Axxb02ax003...

  • Nodejs(Express) - 02 路由

    路由定义了哪个url由哪个函数响应。url请求包含URI地址和请求方式(Get,Post等)Express有几种路...

  • ARouter路由解析

    目录介绍 01.原生跳转实现 02.实现组件跳转方式2.1 传统跳转方式2.2 为何需要路由 03.ARouter...

网友评论

      本文标题:02 路由

      本文链接:https://www.haomeiwen.com/subject/cybszftx.html