•  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
  • 顶级加密软件的Windows
  • Tutanota "blocking" on Comcast Network sparks the debate of net neutrality again
  • The ELK Stack: Elasticsearch, Logstash, Kibana.
  • Scaling your Business with a Dedicated Server and Containers
  • Psychz Networks Moves into Seoul, South Korea to Launch A Data Center
  • KVM Server in USA
  • Icinga vs Nagios
  • How to order DNS Service
  • How to Purge Cache
  • Fully dedicated high end server hosting?
  • DDoS攻击勒索:网络犯罪的最新罪恶扭曲
  • Create Reseller Account in WHM
  • CDN Cache Intro
  • Best Dedicated Server Hosting With DDoS Protection
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