Node.js基础9:web 服务器1 请求与响应、路由

41次阅读

共计 6115 个字符,预计需要花费 16 分钟才能阅读完成。

web 服务器
1 介绍
客户端服务器模块
var http = require(‘http’)
var onRequest = function (request,response) {
// 这个函数的参数分别是请求和响应, 这两个参数都实现了流, 都是流的一个实例, 可以像流一样操作
//request 是浏览器发送过来的请求,response 是要发给浏览器的响应
console.log(“Request Received”);
response.writeHead(200,{‘Content-Type’:’text/plain’})// 写入流, 写头部信息, 第一个参数是状态码. 第二个参数代表要穿给浏览器的内容的类型
//{‘Content-Type’:’text/plain’}代表告诉浏览器这是纯文本内容, 浏览器就理解了如何渲染他, 如何读取他
// response.write(‘hello from out application’)
//response.end()
response.end(‘hello from out application’)// 上面两句或者这样写
}
var server = http.createServer(onRequest)
server.listen(3000,’127.0.0.1′)// 在 3000 端口监听请求
console.log(‘Server started on localhost port 3000’)
然后 node app.js 开启服务器本地访问 127.0.0.1:3000
2 响应 json
响应 json
var http = require(‘http’);

var onRequest = function(request, response) {
console.log(‘Request received’);
response.writeHead(200, { ‘Content-Type’: ‘application/json’});
// response.write(‘Hello from out application’);
var myObj = {
name: “hfpp2012”,
job: “programmer”,
age: 27
};
response.end(JSON.stringify(myObj));
}

var server = http.createServer(onRequest);

server.listen(3000, ‘127.0.0.1’);
console.log(‘Server started on localhost port 3000’);
3 响应 HTML
app.js
var onRequest = function(request, response) {
console.log(‘Request received’);
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
var myReadStream = fs.createReadStream(__dirname + ‘/index.html’, ‘utf8’);
// response.write(‘Hello from out application’);
myReadStream.pipe(response);// 可以直接用流和管道方便些
}

var server = http.createServer(onRequest);

server.listen(3000, ‘127.0.0.1’);
console.log(‘Server started on localhost port 3000’);
假设这是相同文件路径下的 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>xxx</title>
</head>

<body>
hello wolrd
</body>
4 用模块化思想组织代码
将服务器代码单独当成一个模块分离出来 server.js
var http = require(‘http’);
var fs = require(‘fs’);

function startServer() {
var onRequest = function(request, response) {
console.log(‘Request received’);
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
var myReadStream = fs.createReadStream(__dirname + ‘/index.html’, ‘utf8’);
// response.write(‘Hello from out application’);
myReadStream.pipe(response);
}

var server = http.createServer(onRequest);

server.listen(3000, ‘127.0.0.1’);
console.log(‘Server started on localhost port 3000’);
}

exports.startServer = startServer;
app.js
var server = require(‘./server’);

server.startServer();
5 路由
例如我们访问一个网站,www.123.com/index 这个 /index 就是路由, 访问不同的页面路由就不一样, 后台处理的方式也不一样, 这样用来请求不同的资源.request.url 获取浏览器传过来的路由信息, 然后根据路由进行判断 server.js
var http = require(‘http’);
var fs = require(‘fs’);

function startServer() {
var onRequest = function(request, response) {
console.log(‘Request received ‘ + request.url);//request.url 获取浏览器传过来的路由信息, 然后根据路由进行判断
if (request.url === ‘/’ || request.url === ‘/home’) {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/index.html’, ‘utf8’).pipe(response);
} else if (request.url === ‘/review’) {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/review.html’, ‘utf8’).pipe(response);
} else if (request.url === ‘/api/v1/records’) {
response.writeHead(200, { ‘Content-Type’: ‘application/json’});
var jsonObj = {
name: “hfpp2012″
};
response.end(JSON.stringify(jsonObj));
} else {
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’);
}

exports.startServer = startServer;
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>
6 重构路由代码
app.js
var server = require(‘./server’);
var router = require(‘./router’);
var handler = require(‘./handler’);

var handle = {};
handle[“/”] = handler.home;
handle[‘/home’] = handler.home;
handle[‘/review’] = handler.review;
handle[‘/api/v1/records’] = handler.api_records;

server.startServer(router.route, handle);
server.js
var http = require(‘http’);
var fs = require(‘fs’);

function startServer(route, handle) {
var onRequest = function(request, response) {
console.log(‘Request received ‘ + request.url);
route(handle, request.url, response);
}

var server = http.createServer(onRequest);

server.listen(3000, ‘127.0.0.1’);
console.log(‘Server started on localhost port 3000’);
}

module.exports.startServer = startServer;
router.js
var fs = require(‘fs’);

function route(handle, pathname, response) {
console.log(‘Routing a request for ‘ + pathname);
if (typeof handle[pathname] === ‘function’) {
handle[pathname](response);
} else {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/404.html’, ‘utf8’).pipe(response);
}
}

module.exports.route = route;
handler.js
var fs = require(‘fs’);

function home(response) {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/index.html’, ‘utf8’).pipe(response);
}

function review(response) {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/review.html’, ‘utf8’).pipe(response);
}

function api_records(response) {
response.writeHead(200, { ‘Content-Type’: ‘application/json’});
var jsonObj = {
name: “hfpp2012”
};
response.end(JSON.stringify(jsonObj));
}

module.exports = {
home: home,
review: review,
api_records: api_records
}
解析: 将 server router handle 分别分离, 各自掌管不同的功能
if (request.url === ‘/’ || request.url === ‘/home’) {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/index.html’, ‘utf8’).pipe(response);
} else if (request.url === ‘/review’) {
response.writeHead(200, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/review.html’, ‘utf8’).pipe(response);
} else if (request.url === ‘/api/v1/records’) {
response.writeHead(200, { ‘Content-Type’: ‘application/json’});
var jsonObj = {
name: “hfpp2012”
};
response.end(JSON.stringify(jsonObj));
} else {
response.writeHead(404, { ‘Content-Type’: ‘text/html’});
fs.createReadStream(__dirname + ‘/404.html’, ‘utf8’).pipe(response);
}
}
router 相当于上面这段代码的 if()else(), 起到: 根据不同路径, 判断该走哪条路的作用.handle 根据不同的路, 来执行不同的方法. 相当于 ifelse 中 {} 内执行的代码

正文完
 0