企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 一、概述 使用 Node.js 时,我们不仅仅 在实现一个应用,同时还实现了整个 HTTP 服务器。 Node.js 应用是由哪几部分组成的: 1. 引入 required 模块:我们可以使用**require**指令来载入 Node.js 模块。 2. 创建服务器:**服务器可以监听客户端的请求,类似于 Apache 、Nginx 等 HTTP 服务器。 3. 接收请求与响应请求:服务器很容易创建,客户端可以使用浏览器或终端发送 HTTP 请求,服务器接收请求后返回响应数据。 ## 二、典型应用 ``` var http = require('http');// 1、引入 required 模块 http.createServer(function (request, response) { // 发送 HTTP 头部 // HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 发送响应数据 "Hello World" response.end('Hello World\n'); }).listen(8888);//2、创建服务器 console.log('Server running at http://127.0.0.1:8888/');// 3、接收请求与响应请求 ``` 使用**node**命令执行以上的代码: ``` node testnode.js ``` ![](https://img.kancloud.cn/02/95/029507ab43c40e3016c9b633aad6440c_677x443.png)