本文已参与「新人创作礼」活动,一起开启掘金创作之路。
文章目录
前言
上一篇文章我们介绍了nodejs的web 服务器 part 4 用模块化思想组织代码,详见:Node.js后端开发 - 基础篇 #12 web 服务器 part 4 用模块化思想组织代码,这篇文章我们将来介绍一下nodejs的路由,那么什么是路由呢?
什么是nodejs的路由?
我们来看一个实例,如我们打开csdn官网:CSDN博客 - 专业IT技术发表平台,我们可以看到页面,如下
编辑
进入官网 推荐 默认对应的地址为:CSDN博客 - 专业IT技术发表平台,如果你要查看其他资料,如查看 关注、程序人生 等等 , 它们的地址会变为:blog.csdn.net/nav/watcher…、blog.csdn.net/nav/career等等,如下图:
编辑
编辑
其中这些:/nav/watchers、/nav/career 就是路由,路由不同那它请求的资源就不一样,然后我们后台处理的方式也不一样!
路由实例编码
接下来我们来写路由,让它进入不同的路由,请求不同的资源!那么首先是浏览器传入不同的路由过来,那么我们后端要根据路由进行不同的处理,也就是说给它响应不同的内容!那么第一步就要获取到浏览器传过来的路由信息,根据它的路由进行判断,那么怎么获取到传过来的路由信息呢?我们开始编码,app.js里面的代码没有变化,如下:
//require导入server.js模块
var server = require('./server');
//调用server.js模块的startServer函数
server.startServer();
server.js文件代码修改成如下:
var http = require('http');
var fs = require('fs');
//把启动server、响应客户端的代码,放在一个函数里面
function startServer() {
var onRequest = function (request, response) {
console.log('Request received ' + request.url);
if (request.url === '/' || request.url === '/home') {
//其实是对这些地址的判断:http://localhost:3000/、 http://localhost:3000/home
// 然后显示 index.html 页面
response.writeHead(200, { 'Content-Type': 'text/html' });
fs.createReadStream(__dirname + '/index.html', 'utf8').pipe(response);
} else if (request.url === '/review') {
//其实是对这个地址的判断:http://localhost:3000/review
// 然后显示 review.html 页面
response.writeHead(200, { 'Content-Type': 'text/html' });
fs.createReadStream(__dirname + '/review.html', 'utf8').pipe(response);
} else if (request.url === '/api/v1/records') {
//其实是对这个地址的判断:http://localhost:3000/api/v1/records
// 然后显示 json数据
response.writeHead(200, { 'Content-Type': 'application/json' });
var jsonObj = {
name: "yyh"
};
response.end(JSON.stringify(jsonObj));
} else {//其它路由就显示 404.html页面
response.writeHead(404, { 'Content-Type': 'text/html' });
fs.createReadStream(__dirname + '/404.html', 'utf8').pipe(response);
}
}
var server = http.createServer(onRequest);
server.listen(3000, '127.0.0.1');
console.log('Server started on localhost port 3000');
}
// 暴露startServer函数
//下面的代码,等同于 module.exports.startServer = startServer;
exports.startServer = startServer;
当我们启动server端,然后在浏览器中输入:http://localhost:3000/、http://localhost:3000/home、http://localhost:3000/review等,然后我们的终端会得到并输入以下信息:
MacBook-Pro:hello-nodejs luminal$ node app
Server started on localhost port 3000
Request received /
Request received /favicon.ico
Request received /home
Request received /favicon.ico
Request received /review
Request received /favicon.ico
我们会发现,代码中 request.url 会得到客户端的路由,/ 、 /home、/review 它们依次对应上面三个路径( 这个浏览器默认都会去请求favicon.ico图标,这个我们先忽略! )
所以才有后面的 if判断语句。。。, 我们可以启动sever端,依次在浏览器输入如下地址查看效果,具体效果我就不贴图了!!!
http://localhost:3000/ 显示index.html
http://localhost:3000/home 显示index.html
http://localhost:3000/review 显示review.html
http://localhost:3000/api/v1/records 显示json数据
http://localhost:3000/api/else 显示404.html
附上相关测试的html代码
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>这是一个html页面</title>
</head>
<body>
hello wolrd hello wolrd
</body>
</html>
review.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
review page
</body>
</html>
404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
404 error page
</body>
</html>