淺析用Node建立一個簡單的HTTP伺服器
怎麼使用NodeJS建立HTTP伺服器?以下這篇文章跟大家介紹一下使用Node建立一個簡單的HTTP伺服器的方法,希望對大家有幫助!
1. 使用Node.js直接執行JavaScript腳本
node.js
基於Chrome
的v8
引擎運行js
程式碼,因此我們可以擺脫瀏覽器環境,直接在控制台中執行js
程式碼,例如下面這個hello world
程式碼
console.log('hello world');
控制台中直接使用node
即可執行
2. 建立一個簡單的HTTP伺服器
node.js
的內建模組http
提供了基本的http
服務的能力,基於CommonJS
規範,我們可以使用require
導入http
模組進行使用http
模組中有一個createServer
函數能夠讓我們建立一個http
伺服器
其接收一個回呼函數作為參數,這個回呼函數接收兩個參數 -- request
和response
。 【相關教學推薦:nodejs影片教學】
-
request
包含所有客戶端請求的信息,例如url
、請求頭header
、請求方式和請求體等 -
response
主要用於傳回訊息給客戶端,封裝了一些操作回應體相關的操作,例如response.writeHead
方法就可以讓我們自訂返回體的頭部資訊和狀態碼
當我們將回應體處理好了之後,呼叫response.end()
方法就可以將回應體傳送給客戶端
使用createServer
函數只是幫我們建立了一個Server
對象,並沒有讓其開啟監聽,我們還需要呼叫server
對象的listen
方法才可以進行監聽,真正作為一個伺服器開始運行
-
listen
方法的第一個參數是監聽的連接埠號,第二個參數則是綁定的主機ip
,第三個參數是一個回呼函數,會被http
模組非同步調用,當遇到錯誤的時候,就能夠在這個回呼函數的第一個參數中取得到拋出的異常,我們可以選擇對異常進行處理,讓我們的伺服器更加健壯
#下面是使用http
模組建立一個簡單伺服器的範例
const { createServer } = require('http'); const HOST = 'localhost'; const PORT = '8080'; const server = createServer((req, resp) => { // the first param is status code it returns // and the second param is response header info resp.writeHead(200, { 'Content-Type': 'text/plain' }); console.log('server is working...'); // call end method to tell server that the request has been fulfilled resp.end('hello nodejs http server'); }); server.listen(PORT, HOST, (error) => { if (error) { console.log('Something wrong: ', error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
可以直接嘗試用node
運行它,創造一個屬於你的伺服器!伺服器執行後,瀏覽器造訪http://localhost:8080即可存取到這個伺服器
也可以使用nodemon
運行它,這樣當我們的程式碼改變的時候就不需要手動終止程式再重新運行了
npm i -g nodemon
建議全域安裝它,這樣就可以直接使用,不需要透過npx nodemon
去使用
使用也很簡單,直接將node
指令改成nodemon
指令即可
nodemon http-server.js
##3 . 加上類型提示
前面我們在使用createServer以及
resp物件的時候,看不到任何的語法提示,必須隨時跟著
node官方文件去邊用邊查,有點不方便
但沒關係,我們可以使用
ts的
.d.ts檔案來幫助我們提供語法提示功能,注意,我們不是使用
ts進行開發,只是使用它的語法提示功能而已
- 初始化項目--
- npm init -y
- @types/node
# --
pnpm i @types/node -D 在專案目錄下建立 - jsconfig.json
文件,將
node_modules排除在外,沒必要對其進行檢查
{ "compilerOptions": { "checkJs": true }, "exclude": ["node_modules", "**/node_modules/*"] }
checkJs能夠幫助我們檢查類型錯誤問題,可以根據需要選擇是否開啟
可以看到,開啟檢查後立刻就給我們提示了參數類型不符的問題
#
这时候将鼠标悬浮在listen
方法上,就能够看到该方法的签名
可以看到,原来port
参数需要是number
类型,但是我们定义的时候是string
类型,所以没匹配上,将其修改为number
的8080
即可
而且可以直接查看到相关api
的文档,不需要打开node
官方的文档找半天去查看了
4. 返回多个字符串的响应体
前面我们的简单http server
中只返回了一句话,那么是否能够返回多句话呢?
这就要用到resp
对象的write
方法了,end
只能够返回一次内容,而是用write
方法,我们可以多次写入内容到响应体中,最后只用调用一次end
,并且不传递任何参数,只让他完成发送响应体的功能
const { createServer } = require("http"); const HOST = "localhost"; const PORT = 8080; const server = createServer((req, resp) => { resp.writeHead(200, { "Content-Type": "text/plain" }); console.log("server is working..."); // write some lorem sentences resp.write("Lorem ipsum dolor sit amet consectetur adipisicing elit.\n"); resp.write("Omnis eligendi aperiam delectus?\n"); resp.write("Aut, quam quo!\n"); resp.end(); }); server.listen(PORT, HOST, (error) => { if (error) { console.log("Something wrong: ", error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
这次我们写入了三句话,现在的效果就变成这样啦
5. 返回html
我们不仅可以返回字符串给浏览器,还可以直接读取html
文件的内容并将其作为结果返回给浏览器
这就需要用到另一个Node.js
的内置模块 -- fs
,该模块提供了文件操作的功能
使用fs.readFile
可以异步进行读取文件的操作,但是它不会返回promise
对象,因此我们需要传入回调去处理读取到文件后的操作
还可以使用fs.readFileSync
进行同步阻塞读取文件,这里我们选择异步读取
const { createServer } = require("http"); const fs = require("fs"); const HOST = "localhost"; const PORT = 8080;const server = createServer((req, resp) => { // change the MIME type from text/plain to text/html resp.writeHead(200, { "Content-Type": "text/html" }); // read the html file content fs.readFile("index.html", (err, data) => { if (err) { console.error( "an error occurred while reading the html file content: ", err ); throw err; } console.log("operation success!"); resp.write(data); resp.end(); }); }); server.listen(PORT, HOST, (error) => { if (error) { console.log("Something wrong: ", error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
现在的结果就像下面这样:
成功将html
返回注意:这里需要将响应头的**Content-Type**
改为**text/html**
,告知浏览器我们返回的是**html**
文件的内容,如果仍然以**text/plain**
返回的话,浏览器不会对返回的内容进行解析,即便它是符合**html**
语法的也不会解析,就像下面这样:
6. 返回JSON
当我们需要编写一个后端服务器,只负责返回接口数据的时候,就需要返回json
格式的内容了,相信聪明的你也知道该怎么处理了:
- 将
MIME
类型设置为application/json
-
resp.write
的时候传入的是json
字符串,可以使用JSON.stringify
处理对象后返回
const { createServer } = require("http"); const HOST = "localhost"; const PORT = 8080; const server = createServer((req, resp) => { // change the MIME type to application/json resp.writeHead(200, { "Content-Type": "application/json" }); // create a json data by using an object const jsonDataObj = { code: 0, message: "success", data: { name: "plasticine", age: 20, hobby: "coding", }, }; resp.write(JSON.stringify(jsonDataObj)); resp.end(); }); server.listen(PORT, HOST, (error) => { if (error) { console.log("Something wrong: ", error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
结果如下:
7. 返回pdf文件
和之前返回html
文件的思路类似,都是一个设置响应头MIME
类型,读取文件,返回文件内容的过程
但是这次我们搞点不一样的
我们的思路是在服务器运行的时候生成一个pdf
文件,并将它返回
还需要将MIME
的类型改为application/pdf
生成pdf
文件需要用到一个库 -- pdfkit
pnpm i pdfkit
首先我们编写一个创建pdf
文件的函数,因为创建pdf
文件还需要进行一些写入操作,不确定什么时候会完成,但是我们的请求必须等到pdf
文件创建完成后才能得到响应
所以我们需要将它变成异步进行的,返回一个promise
/** * @description 创建 pdf 文件 */const createPdf = () => { return new Promise((resolve, reject) => { if (!fs.existsSync("example.pdf")) { // create a PDFDocument object const doc = new PDFDocument(); // create write stream by piping the pdf content. doc.pipe(fs.createWriteStream("example.pdf")); // add some contents to pdf document doc.fontSize(16).text("Hello PDF", 100, 100); // complete the operation of generating PDF file. doc.end(); } resolve("success"); }); };
这里使用到了管道操作,将PDFDocument
对象的内容通过管道传到新创建的写入流中,当完成操作后我们就通过resovle
告知外界已经创建好pdf
文件了
然后在服务端代码中调用
const server = createServer(async (req, resp) => { // change the MIME type to application/pdf resp.writeHead(200, { "Content-Type": "application/pdf" }); // create pdf file await createPdf(); // read created pdf file fs.readFile("example.pdf", (err, data) => { if (err) { console.error( "an error occurred while reading the pdf file content: ", err ); throw err; } console.log("operation success!"); resp.end(data); }); }); server.listen(PORT, HOST, (error) => { if (error) { console.log("Something wrong: ", error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
现在浏览器就可以读取到创建的pdf
文件了
8. 返回音频文件
思路依然是一样的,读取一个音频文件,然后通过管道将它送到resp
对象中再返回即可
const { createServer } = require("http"); const { stat, createReadStream } = require("fs"); const HOST = "localhost"; const PORT = 8080; const server = createServer((req, resp) => { // change the MIME type to audio/mpe resp.writeHead(200, { "Content-Type": "audio/mp3" }); const mp3FileName = "audio.mp3"; stat(mp3FileName, (err, stats) => { if (stats.isFile()) { const rs = createReadStream(mp3FileName); // pipe the read stream to resp rs.pipe(resp); } else { resp.end("mp3 file not exists"); } }); }); server.listen(PORT, HOST, (error) => { if (error) { console.log("Something wrong: ", error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
效果如下
打开后就是一个播放音频的界面,这是chrome
提供的对音频文件的展示,并且打开控制台会发现有返回音频文件
注意:将音频文件流通过管道传到**resp**
后,不需要调用**resp.end()**
方法,因为这会关闭整个响应,导致音频文件无法获取
9. 返回视频文件
视频文件和音频文件的处理是一样的,只是MIME
的类型要改成video/mp4
,其他都一样
const { createServer } = require("http"); const { stat, createReadStream } = require("fs"); const HOST = "localhost"; const PORT = 8080; const server = createServer((req, resp) => { // change the MIME type to audio/mpe resp.writeHead(200, { "Content-Type": "audio/mp4" }); const mp4FileName = "video.mp4"; stat(mp4FileName, (err, stats) => { if (stats.isFile()) { const rs = createReadStream(mp4FileName); // pipe the read stream to resp rs.pipe(resp); } else { resp.end("mp4 file not exists"); } }); }); server.listen(PORT, HOST, (error) => { if (error) { console.log("Something wrong: ", error); return; } console.log(`server is listening on http://${HOST}:${PORT} ...`); });
总结
我们学会了:
- 如何使用
Node
创建一个http
服务器 - 给
js
加上类型提示 - 如何返回字符串响应体
- 如何返回
html
- 如何返回
JSON
- 如何生成并返回
pdf
文件 - 如何返回音频文件
- 如何返回视频文件
虽然内容简单,但还是希望你能跟着动手敲一敲,不要以为简单就看看就算了,看了不代表会了,真正动手实现过后才会找到自己的问题
更多node相关知识,请访问:nodejs 教程!
以上是淺析用Node建立一個簡單的HTTP伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

基於無阻塞、事件驅動建立的Node服務,具有記憶體消耗低的優點,非常適合處理海量的網路請求。在海量請求的前提下,就需要考慮「記憶體控制」的相關問題了。 1. V8的垃圾回收機制與記憶體限制 Js由垃圾回收機

怎麼處理文件上傳?以下這篇文章為大家介紹一下node專案中如何使用express來處理文件的上傳,希望對大家有幫助!

PiNetwork節點詳解及安裝指南本文將詳細介紹PiNetwork生態系統中的關鍵角色——Pi節點,並提供安裝和配置的完整步驟。 Pi節點在PiNetwork區塊鏈測試網推出後,成為眾多先鋒積極參與測試的重要環節,為即將到來的主網發布做準備。如果您還不了解PiNetwork,請參考Pi幣是什麼?上市價格多少? Pi用途、挖礦及安全性分析。什麼是PiNetwork? PiNetwork項目始於2019年,擁有其專屬加密貨幣Pi幣。該項目旨在創建一個人人可參與

這篇文章跟大家分享Node的進程管理工具“pm2”,聊聊為什麼需要pm2、安裝和使用pm2的方法,希望對大家有幫助!

文件模組是對底層文件操作的封裝,例如文件讀寫/打開關閉/刪除添加等等文件模組最大的特點就是所有的方法都提供的**同步**和**異步**兩個版本,具有sync 字尾的方法都是同步方法,沒有的都是異

事件循環是 Node.js 的基本組成部分,透過確保主執行緒不被阻塞來實現非同步編程,了解事件循環對建立高效應用程式至關重要。以下這篇文章就來帶大家深入了解Node中的事件循環 ,希望對大家有幫助!

身份驗證是任何網路應用程式中最重要的部分之一。本教程討論基於令牌的身份驗證系統以及它們與傳統登入系統的差異。在本教程結束時,您將看到一個用Angular和Node.js編寫的完整工作演示。傳統身份驗證系統在繼續基於令牌的身份驗證系統之前,讓我們先來看看傳統的身份驗證系統。使用者在登入表單中提供使用者名稱和密碼,然後點擊登入。發出請求後,透過查詢資料庫在後端驗證使用者。如果請求有效,則使用從資料庫中獲取的使用者資訊建立會話,然後在回應頭中傳回會話訊息,以便將會話ID儲存在瀏覽器中。提供用於存取應用程式中受
