•  Home
  •  Dashboard
  •  Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
  •  Hosting Services
    • Infrastructure
      • iColocation
    • Compute
      • cMetal
      • cVirtual
    • Storage
      • sObject
      • sBlock
    • Networking
      • nCDN
      • nIP Transit
      • nWavelength
    • Protection
      • pBackup
      • pDDoS
  •  Solutions
    • Ecommerce
    • Finance
    • Gaming
    • Hosting
    • Management
    • Security
    • System Integrator
  •  Support
    • Community
    • Knowledge Base
    • Open A Ticket
  •  USA & Canada: 800-933-1517
  •  International: 626-549-2801
  •  Email: sales@psychz.net
  • Services
    • new-colocation-header-img
      Infrastructure
      • iColocation
    • new-compute-header-img
      Compute
      • cMetal
      • cVirtual
    • new-storage-header-img
      Storage
      • sObject
      • sBlock
    • new-networking-header-img
      Networking
      • nCDN
      • nIP Transit
      • nWavelength
    • new-protection-header-img
      Protection
      • pBackup
      • pDDoS
  • Solutions
    • Ecommerce
    • Security
    • Gaming
    • Hosting
    • Management
    • Finance
    • System Integrator
  • Dashboard

Nodejs https服務器

  • Home
  • Client
  • Qa Forum
  • Nodejs https服務器

Posted By: SHENGHUA | 1 Replies | Last Reply On: Jul 20, 2017 06:12:58

我有一個專門的服務器,我最近安裝了Ubuntu 14.04。請問如何創建一個可以處理HTTP請求的簡單Node.js Web服務器的步驟。

Psychz - Shrirang

Votes: 0Posted On: Jul 20, 2017 06:12:58
 

Node.js是一個基於Chrome V8 JavaScript引擎構建的JavaScript運行時。這意味著Node.js提供了一個在服務器上運行JavaScript的平台。因此,我們不需要基於服務器的語言來開發Web應用程序。您可以使用Node.js應用程序來創建自己的HTTP服務器。

以下是引導您創建自己的HTTP服務器的步驟。

安裝Node.js

請在終端上運行以下命令來安裝當前版本。

sudo apt-get install python-software-properties
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install nodejs

2.您還可以訪問Nde.js網站並安裝當前版本。

https://nodejs.org/en/

創建自己的HTTP服務器

創建一個擴展名為“.js”的空文件。我們正在為我們的示例創建一個名為“Hello.js”的文件。使用以下命令進行以下操作。

sudo touch Hello.js

創建文件後,編輯文件並輸入以下代碼。

var http = require('http')
var server = http.createServer(function(request,response)
{
response.writeHead(200,{'Content-Type':'text / plain'});
response.end(“你已經輸入”);
});
server.listen(80);

該代碼的功能是它讀取JavaScript,執行它並返回對象。讓我們指導你一行一行的代碼。

1.我們包括HTTP模塊,從中獲取所需的函數,以便它們可以在應用程序中使用。

2.我們創建一個具有“請求和響應”功能的服務器應用程序。每次用戶請求一個URL時都會調用“請求”功能。 “響應”功能是我們對請求的響應。

3.收到請求時,我們會發送一個標題類型為“200.”的回复。這個數字是當向客戶端發送成功的響應時在http頭髮送的正常響應。 “內容類型”是文本。因此,響應應以文本格式。

這是對請求的回應。它將在屏幕上顯示“您已輸入”並結束響應。

5.服務器將監聽端口80,這是默認的HTTP端口。但是,這不是強制性的。

最後一步

完成代碼編寫後,鍵入以下命令以使HTTP服務器正常運行。

節點Hello.js

您可以通過瀏覽器檢查HTTP服務器的工作。當您輸入“localhost”時,將顯示消息“您已輸入”。

Was this reply helpful?

Related topics

  • How to install Node.js on AlmaLinux 8
  • 頂部神話關於Linux安全
  • 為Linux頂級殺毒程序
  • 基本和有用的Linux命令
  • 倫敦託管價格
  • sObject - Delete
  • The Role of Dedicated Servers in Disaster Recovery and Business Continuity.
  • Semi Dedicated Hosting Windows Server 2012 R2
  • How to setup a Dedicated server for Risk of Rain 2
  • How to check RAM for error using Ubuntu Live CD?
  • Hotlink Protection
  • COVID-19's Impact On Network And Compute Hardware
  • 10Gbps USA Dedicated Servers
  • 1000 Gbps DDoS Protection
  • 1 Month Trial Dedicated Server
Copyright © 2025 Psychz Networks,
A Profuse Solutions Inc Company
Hosting Services
Infrastructure
  • iColocation
Compute
  • cMetal
  • cVirtual
Storage
  • sObject
  • sBlock
Networking
  • nCDN
  • nIP Transit
  • nWavelength
Protection
  • pBackup
  • pDDoS
Company
  • About Us
  • Blog
  • Careers
  • Contact Us
  • Data Centers
  • Looking Glass
  • Network
  • Reseller
Policies
  • Acceptable Usage Policy
  • Privacy Policy
  • Service Level Agreement
  • Terms and Conditions
Support
  • Community
  • Knowledge Base
  • Open A Ticket
Get In Touch
  • Psychz Networks,
    A Profuse Solutions Company
    611 Wilshire Blvd #300
    Los Angeles,California 90017
    USA
  • US/Canada: 800-933-1517
  • International: 626-549-2801