Node.js創(chuàng)建Web、TCP服務(wù)器
使用http模塊創(chuàng)建Web服務(wù)器
Web服務(wù)器的功能:
接受HTTP請求(GET、POST、DELETE、PUT、PATCH)
處理HTTP請求(自己處理,或請求別的程序處理)
做出響應(yīng)(返回頁面、文件、各類數(shù)據(jù)等)
常見的Web服務(wù)器架構(gòu):
Nginx、Apache:負(fù)責(zé)接受HTTP請求,確定誰來處理請求,并返回請求的結(jié)果
php-fpm / php模塊:處理分配給自己的請求,并將處理結(jié)果返回給分配者
常見請求種類:
請求文件:包括靜態(tài)文件(網(wǎng)頁、圖片、前端JavaScript文件、css文件...),及由程序處理得到的文件
完成特定的操作:如登錄、獲取特定數(shù)據(jù)等
Node.js的Web服務(wù)器:
不依賴其他特定的Web服務(wù)器軟件(如Apache、Nginx、IIS......)
Node.js代碼處理請求的邏輯
Node.js代碼負(fù)責(zé)Web服務(wù)器的各種“配置”
使用Express創(chuàng)建Web服務(wù)器
簡單的Express服務(wù)器
靜態(tài)文件服務(wù)
路由
中間件
簡單的Express服務(wù)器:
var express = require('express');
var app = express();
app.get('', function(req, res){
<span style="white-space:pre"> </span>res.end('hello\n');
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>app.listen(18001, function afterListen(){
<span style="white-space:pre"> </span>console.log('express running on http://localhost:18001');
<span style="white-space:pre"> </span>});
靜態(tài)文件范圍:
網(wǎng)頁、純文本、圖片、前端JavaScript代碼、CSS樣式表文件、媒體文件、字體文件
使用Express訪問靜態(tài)文件
<span style="white-space:pre"></span>app.use(express.static('./public'));
路由:
將不同的請求,分配給相應(yīng)的處理函數(shù)
區(qū)分:路徑、請求方法
三種路由實(shí)現(xiàn)方法:
path:比較簡單
Router:比較適合同一個(gè)路由下的多個(gè)子路由
route:比較適合API
中間件
Connect:Node.js的中間件框架
分層處理
每層實(shí)現(xiàn)一個(gè)功能
創(chuàng)建TCP服務(wù)器
使用net模塊創(chuàng)建TCP服務(wù)器
使用telnet連接TCP服務(wù)器
使用net創(chuàng)建TCP客戶端
利用node.js搭建簡單web服務(wù)器JS代碼部分:
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
var dir, arg = process.argv[2] || ''; // 命令行第三個(gè)參數(shù),用來接收目錄,可為空,相對(duì)當(dāng)前server.js文件的目錄名稱
// 比如使用命令 node server debug,意思就是debug文件夾與server.js文件同級(jí)
// 且你想以debug文件夾啟動(dòng)web服務(wù)
http.createServer(function (req, res) {
var pathname = __dirname + url.parse(req.url).pathname;
dir = dir ? dir : pathname; // 記住dir(目錄)
pathname = dir ? pathname.replace(dir, dir + arg + '/') : pathname; // 替換文件靜態(tài)路徑
if (path.extname(pathname) == "") {
pathname += "/";
}
if (pathname.charAt(pathname.length - 1) == "/") {
pathname += "index.html"; // 入口文件,此處默認(rèn)index.html
}
fs.exists(pathname, function (exists) {
if (exists) {
switch (path.extname(pathname)) {
case ".html":
res.writeHead(200, {"Content-Type": "text/html"});
break;
case ".js":
res.writeHead(200, {"Content-Type": "text/javascript"});
break;
case ".css":
res.writeHead(200, {"Content-Type": "text/css"});
break;
case ".gif":
res.writeHead(200, {"Content-Type": "image/gif"});
break;
case ".jpg":
res.writeHead(200, {"Content-Type": "image/jpeg"});
break;
case ".png":
res.writeHead(200, {"Content-Type": "image/png"});
break;
default:
res.writeHead(200, {"Content-Type": "application/octet-stream"});
}
// res可以自己添加信息來簡單交互 比如可以修改點(diǎn)header信息 或者修改返回的資源數(shù)據(jù)
fs.readFile(pathname, function (err, data) {
res.end(data);
});
}
else {
res.writeHead(404, {"Content-Type": "text/html"});
res.end("<h1>404 Not Found</h1>");
}
});
}).listen(8085, "127.0.0.5"); // 服務(wù)器端口
console.log("server running at http://127.0.0.5:8085/");
相關(guān)文章
Node.js中環(huán)境變量process.env的一些事詳解
這篇文章主要給大家介紹了關(guān)于Node.js中環(huán)境變量process.env的一些事,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用node.js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
前端必會(huì)的nodejs知識(shí)工具模塊使用示例詳解
這篇文章主要為大家介紹了前端必會(huì)的nodejs知識(shí)工具模塊使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Node.js API詳解之 module模塊用法實(shí)例分析
這篇文章主要介紹了Node.js API詳解之 module模塊用法,結(jié)合實(shí)例形式分析了Node.js API中module模塊基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
Node.js連接MongoDB數(shù)據(jù)庫產(chǎn)生的問題
Node.js是使用JavaScript 編寫的可以運(yùn)行在服務(wù)端的JS語言。node.js和mongodb碰撞會(huì)產(chǎn)生一系列問題,下面通過本文給大家分享Node.js連接MongoDB數(shù)據(jù)庫,需要的的朋友參考下2017-02-02
詳解Node中導(dǎo)入模塊require和import的區(qū)別
本篇文章主要介紹了詳解Node中導(dǎo)入模塊require和import的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08
Node.js對(duì)MongoDB進(jìn)行增刪改查操作的實(shí)例代碼
這篇文章主要介紹了Node.js對(duì)MongoDB進(jìn)行增刪改查操作 ,需要的朋友可以參考下2019-04-04

