nodejs 開發web,node.js 創建服務器_Node.js HTTP軟件包–創建HTTP服務器

 2023-11-19 阅读 28 评论 0

摘要:node.js 創建服務器An HTTP server caters to client requests and sends appropriate response. For example, JournalDev is also running on HTTP server. If you have landed here directly, I would suggest you to go through earlier posts about NodeJS IO Package an

node.js 創建服務器

An HTTP server caters to client requests and sends appropriate response. For example, JournalDev is also running on HTTP server. If you have landed here directly, I would suggest you to go through earlier posts about NodeJS IO Package and NodeJS Modules because we will use that knowledge here to create a simple HTTP server Node.js application.

HTTP服務器可滿足客戶端請求并發送適當的響應。 例如,JournalDev也正在HTTP服務器上運行。 如果您直接登陸這里,我建議您閱讀有關NodeJS IO包和NodeJS模塊的早期文章,因為我們將在這里使用該知識來創建一個簡單的HTTP服務器Node.js應用程序。

In this post, we are going to discuss about Node JS Platform “HTTP” module to develop HTTP server.

在本文中,我們將討論有關Node JS Platform“ HTTP”模塊以開發HTTP服務器的信息。

  • Introduction to Node JS HTTP Module

    Node JS HTTP模塊簡介
  • Node JS Simple HTTP Server

    節點JS簡單HTTP服務器
  • How Node JS HTTP Server handles Client Requests

    Node JS HTTP Server如何處理客戶端請求
  • Node JS HTTP Server Routings

    節點JS HTTP服務器路由

Node JS HTTP模塊簡介 (Introduction to Node JS HTTP Module)

nodejs 開發web?Implementing an HTTP server is a tough task in Java or any other language frameworks, that’s why we have so many HTTP servers for different platforms such as Apache, Tomcat etc. However, we can develop simple HTTP server by using Node JS Platform very easily. We don’t need to write too much code to develop, up and running a basic HTTP Server.

在Java或任何其他語言框架中,實現HTTP服務器是一項艱巨的任務,這就是為什么我們擁有如此多的HTTP服務器(例如Apache,Tomcat等)的原因。但是,我們可以很容易地使用Node JS Platform開發簡單的HTTP服務器。 我們不需要編寫太多代碼來開發,啟動和運行基本的HTTP Server。

Like some Node modules for example “npm”, “fs” etc., Node JS “http” module is also part of basic Node JS Platform. We don’t need to do anything to setup Node JS HTTP module.

與某些Node模塊(例如“ npm”,“ fs”等)一樣,Node JS“ http”模塊也是基本Node JS平臺的一部分。 我們無需執行任何操作即可設置Node JS HTTP模塊。

We just need to import “http” module and starting writing some code to develop HTTP server.

我們只需要導入“ http”模塊并開始編寫一些代碼來開發HTTP服務器。

node.js怎么安裝,To import a “http” module:

導入“ http”模塊:

var http = require("http");

This require() call imports Node JS HTTP module into cache. Once it’s done, then we can use NODE JS HTTP API to develop HTTP Server.

此require()調用將Node JS HTTP模塊導入緩存。 完成后,我們可以使用NODE JS HTTP API開發HTTP Server。

節點JS簡單HTTP服務器 (Node JS Simple HTTP Server)

Here we will learn how to create a simple HTTP server step by step.

在這里,我們將逐步學習如何創建一個簡單的HTTP服務器。

  • Create a simple Node JS Project, refer Node JS Basic Examples With Node REPL (CLI) and Node IDE for detailed steps.
    node.js-http-server-project

    創建一個簡單的Node JS項目,請參閱帶有Node REPL(CLI)和Node IDE的Node JS基本示例以獲取詳細步驟。
  • Create package.json file with below contents.

    nodejs怎么用。package.json

    {"name": "http-server","version": "1.0.0","description": "Simple HTTP Sever","main": "http-server","author": "JournalDEV","engines":{"node":"*"}
    }

    使用以下內容創建package.json文件。

    package.json

  • Create a JavaScript file with the following content;

    http-server.js

    /*** JournalDEV  * Simple HTTP Server Example*/
    var http = require("http");
    var httpserver = http.createServer();
    httpserver.listen(8001);

    With just few lines of above code, we are able to create a simple HTTP server. Below is the explanation of different parts of the code.

    • First line, require() call loads http module into cache to use Node JS HTTP API.
    • Second line, using HTTP API we are create a Server.
    • Third line, HTTP Server listen at port number: 8001

    Below image shows the project structure for now.

    http-server.js

    /*** JournalDEV  * Simple HTTP Server Example*/
    var http = require("http");
    var httpserver = http.createServer();
    httpserver.listen(8001);

    node是啥, 只需上述幾行代碼,我們就可以創建一個簡單的HTTP服務器。 下面是代碼不同部分的說明。

    • 第一行,require()調用將http模塊加載到緩存中以使用Node JS HTTP API。
    • 第二行,使用HTTP API創建服務器。
    • 第三行,HTTP Server偵聽端口號:8001

    下圖顯示了目前的項目結構。

  • Open command prompt at ${ECLIPSE_WORKSPACE}/http-server project and run “node http-server.js” command.
    node.js-http-server

    If we observe here, command is waiting for client requests that mean our simple Node JS HTTP Server is up and running successfully.

    ${ECLIPSE_WORKSPACE}/http-server項目中打開命令提示符,然后運行“ node http-server.js ”命令。

    如果在這里觀察到,命令正在等待客戶端請求,這意味著我們簡單的Node JS HTTP Server已啟動并成功運行。

However, it cannot handle any requests with this simple HTTP Server. To handle or provide services to Client requests, we need to add some Routings. Please refer next section to extend this Basic HTTP Server program to handle client requests.

但是,它無法使用此簡單的HTTP Server處理任何請求。 要處理或向客戶請求提供服務,我們需要添加一些路由。 請參考下一部分以擴展此基本HTTP Server程序以處理客戶端請求。

Node JS HTTP Server如何處理客戶端請求 (How Node JS HTTP Server handle Client Requests)

node.js配置,Now we are going to add some code to receive and handle HTTP Client request.

現在,我們將添加一些代碼來接收和處理HTTP客戶端請求。

Open http-server.js file in Eclipse IDE and add a JavaScript function to represent request and response objects.

在Eclipse IDE中打開http-server.js文件,并添加一個JavaScript函數來表示請求和響應對象。

/*** JournalDEV  * Simple HTTP Server Example*/
var http = require("http");
var httpserver = http.createServer(function(request,response){});
httpserver.listen(8001);

Here anonymous Function takes request and response as parameters. When an HTTP Client sends a request at port number 8001, this anonymous function will receive that request.

在這里,匿名函數將請求和響應作為參數。 當HTTP客戶端在端口號8001發送請求時,該匿名函數將接收該請求。

java與node。Let’s add some code to send response to the client requests.

讓我們添加一些代碼以發送對客戶端請求的響應。

http-server.js

http-server.js

/*** JournalDEV  * Simple HTTP Server Example*/
var http = require("http");
var httpserver = http.createServer(function(request,response){console.log("Received a Client Request");response.write("Hi, Dear JournalDEV User.")});
httpserver.listen(8001);

Here when HTTP server receives a Client request, it prints a log message at server console and send a response message to the Client.
Just start the server again in the command prompt, if the earlier server is running then you need to close and start it again.

在這里,當HTTP服務器接收到客戶端請求時,它將在服務器控制臺上打印一條日志消息,并將響應消息發送到客戶端。
只需在命令提示符下再次啟動服務器,如果較早的服務器正在運行,則需要關閉并重新啟動它。

model、Now open a browser and go to URL https://localhost:8001/ and you will get below response.

現在打開瀏覽器并轉到URL https://localhost:8001/ ,您將獲得以下響應。

At the same time, you will observe server logs in the command prompt as shown below.

同時,您將在命令提示符下觀察服務器日志,如下所示。

節點JS HTTP服務器路由 (Node JS HTTP Server Routings)

So far we have developed a simple Node JS HTTP Server that just receives HTTP client request and send some response to the server. That means it handles only one kind of request: “/” when client hits this url: “https://localhost:8001”

到目前為止,我們已經開發了一個簡單的Node JS HTTP Server,它僅接收HTTP客戶端請求并將一些響應發送到服務器。 這意味著它僅處理一種請求:當客戶端訪問此URL時,“ /”:“ https:// localhost:8001”

node.js:server-side javascript?It’s not enough to run some real-time applications with this basic HTTP Server. To handle different kinds of Client Requests, we should add “Routings” to our HTTP Server.

用此基本的HTTP Server運行某些實時應用程序還不夠。 要處理各種類型的客戶端請求,我們應該將“路由”添加到HTTP服務器。

Create a new JavaScript file “http-server-simple-routing.js” with following content.

使用以下內容創建一個新JavaScript文件“ http-server-simple-routing.js”。

http-server-simple-routing.js

http-server-simple-routing.js

/*** JournalDEV  * Simple HTTP Server Example*/
var http = require("http");
var url = require('url');var httpserver = http.createServer(
function(request,response){var clientRequestPath = url.parse(request.url).pathname;
switch(clientRequestPath){case '/':console.log("Received a Client Request: Switch Case");response.write("Hi, Dear JournalDEV User: Switch Case.");break;default:console.log("Received a Client Request: Switch Default");response.write("Hi, Dear JournalDEV User:S witch Default.");break;
}response.end();}
);
httpserver.listen(8001);

node創建一個服務器、Here we are using another Node JS Platform Default module: “url”. That means we don’t need to install it manually, it will come with base Node JS Platform installation.

在這里,我們使用另一個Node JS Platform Default模塊:“ url”。 這意味著我們不需要手動安裝它,它將隨基本Node JS Platform安裝一起提供。

Anonymous function with two parameters: request and response. request object contains a property: url, which contains client requested url.

具有兩個參數的匿名函數:請求和響應。 request對象包含一個屬性:url,其中包含客戶端請求的url。

Then we are calling url.parse(request.url).pathname to get client request url. Suppose if Client sends a request by using https://localhost:8001/ url, then here url.parse(request.url).pathname returns “/”.

然后,我們調用url.parse(request.url).pathname來獲取客戶端請求url。 假設如果客戶端通過使用https:// localhost:8001 / url發送請求,則url.parse(request.url).pathname返回“ /”。

nodejs部署前端項目?We are saving the output of url.parse(request.url).pathname value into a local variable: clientRequestPath. We are using switch() block with one case and default blocks. If user sends request using “/”, then server executes switch:case block and send respective response to the client.

我們將url.parse(request.url).pathname值的輸出保存到本地變量:clientRequestPath中。 我們正在將switch()塊與一種情況和默認塊一起使用。 如果用戶使用“ /”發送請求,則服務器執行switch:case塊并將相應的響應發送給客戶端。

If user sends different request, then then server executes switch:default block and send respective response to the client.

如果用戶發送了不同的請求,則服務器將執行switch:default塊并將相應的響應發送給客戶端。

We are closing the Response object using response.end() call.

我們正在使用response.end()調用關閉Response對象。

nodejs項目怎么部署到服務器?Open the Command Prompt/Terminal and start the node http server “http-server-simple-routing.js” and send two different requests from client browser and observe the response.

打開命令提示符/終端,然后啟動節點http服務器“ http-server-simple-routing.js”,并從客戶端瀏覽器發送兩個不同的請求,并觀察響應。

Case 1: Client sends “/” request using https://localhost:8001/ url and you will see below response in browser.

案例1 :客戶端使用https:// localhost:8001 / url發送“ /”請求,您將在瀏覽器中看到以下響應。

Case 2: Client sends “/index.htm” request using https://localhost:8001/index.htm url and gets below response.

情況2 :客戶端使用https:// localhost:8001 / index.htm url發送“ /index.htm”請求,并獲得以下響應。

node服務器的搭建和使用。Below image shows the server logs in these cases.

下圖顯示了在這些情況下的服務器日志。

Here we have gone through some Routings to provide services to Client requests. We will see some more complex routings in next post “Node JS: Socket IO Module”.

在這里,我們已經完成了一些路由來為客戶請求提供服務。 在下一篇文章“ Node JS:套接字IO模塊”中,我們將看到一些更復雜的路由。

To create a simple HTTP Server with simple Routings, we have written some amount of code using Node JS HTTP package. If we use Node JS Express module, then we can create similar kind of Web Servers very easily. We will see those examples in “Node JS: Express Module” in coming posts.

為了創建具有簡單路由的簡單HTTP服務器,我們已經使用Node JS HTTP包編寫了一些代碼。 如果我們使用Node JS Express模塊??,那么我們可以非常輕松地創建類似類型的Web服務器。 我們將在后續文章中的“ Node JS:Express Module”中看到這些示例。

node.js安裝步驟。翻譯自: https://www.journaldev.com/7936/node-js-http-package-creating-http-server

node.js 創建服務器

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183020.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息