日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在node.js中建立你的第一个HTTp服务器

發布時間:2024/4/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在node.js中建立你的第一个HTTp服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這一章節我們將從初學者的角度介紹如何建立一個簡單的node.js HTTP 服務器

?

創建myFirstHTTPServer.js

//Lets require/import the HTTP module var http = require('http');//Lets define a port we want to listen to const PORT=8080; //We need a function which handles requests and send response function handleRequest(request, response){response.end('It Works!! Path Hit: ' + request.url); }//Create a server var server = http.createServer(handleRequest);//Lets start our server server.listen(PORT, function(){//Callback triggered when server is successfully listening. Hurray!console.log("Server listening on: http://localhost:%s", PORT); });

使用node 運行文件

> node myFirstHTTPServer.js#output Server listening on: http://localhost:8080

上述已經在瀏覽器上打開了 http://localhost:8080   

  

分析:

//Lets require/import the HTTP module var http = require('http');

node.js有用于穿件http/https 的 核心模塊,因此我們只需引入http模塊就可創建一個HTTP 服務器

//We need a function which handles requests and send response function handleRequest(request, response){response.end('It Works!! Path Hit: ' + request.url); }

我們需要一個用來處理所有請求和響應的函數

上述代碼是你服務器項目的入口點(即 你可以根據你的業務邏輯來響應請求)

//Create a server var server = http.createServer(handleRequest);//Lets start our server server.listen(PORT, function(){//Callback triggered when server is successfully listening. Hurray!console.log("Server listening on: http://localhost:%s", PORT); });

上述,創建了一個吸金的HTTP服務器對象,并要求其監聽一個端口

createServer方法用來創建一個新的服務器實例并且使用監聽函數作為參數

然后,我們即可調用監聽在服務器上的函數來啟動服務器

?

?

上述是基本的HTTP服務器運行

現在我們添加一些實際需求

?

對于不同的URL路徑,你的服務器應該有不同的響應

這意味著我們需要一個dispatcher

dispatcher是一種路由(router),在針對不同的指定URL路徑時,你可用其來調用你想調用的請求處理函數代碼。

第一:安裝dispatcher 模塊

有很多不同的模塊,針對演示例子,我們安裝了基本的模塊

> npm install httpdispatcher

注意:nmp是一個包管理器,其提供了針對js和nodeJs的核心開源模塊。我們使用 ‘npm install ’命令就可安裝所有所需的模塊

下面,使用dispatcher模塊

var dispatcher = require('httpdispatcher');

接著,讓dispatcher在監聽函數中運行

//Lets use our dispatcher function handleRequest(request, response){try {//log the request on console console.log(request.url);//Disptach dispatcher.dispatch(request, response);} catch(err) {console.log(err);} }

讓我們定義一些路由

路由定義了當瀏覽器請求一個特地的URL時,服務器應該做什么

//For all your static (js/css/images/etc.) set the directory name (relative path). dispatcher.setStatic('resources');//A sample GET request dispatcher.onGet("/page1", function(req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Page One'); }); //A sample POST request dispatcher.onPost("/post1", function(req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Got Post Data'); });

現在讓我們嘗試不同的地址:

  • GET /page1 => 'Page One'
  • POST /page2 => 'Page Two'
  • GET /page3 => 404
  • GET /resources/images-that-exists.png => Image resource
  • GET /resources/images-that-does-not-exists.png => 404

 你可通過在瀏覽器地址欄輸入URL來進行一個get請求。針對Post請求,你可使用Postman工具

?

ok!你現在已經可以建立一個簡單的HTTP服務器并且使之運行了!

上述我們創建了一個基本的HTTP服務器,其創建是通過使用 http模塊和 一個簡單的dispatcher模塊 來實現的, displatcher是用來分配(dispatch)http請求到合適的路由上。

?

?

  

轉載于:https://www.cnblogs.com/RachelChen/p/6604369.html

總結

以上是生活随笔為你收集整理的在node.js中建立你的第一个HTTp服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。