NodeJS初识
Node.JS组成
- 引入required模块: 我们可以使用require执行来载入Node.js模块
- 创建服务器: 服务器可以监听客户端的请求,类似于Apache、Nginx等HTTP服务器
- 接受请求与相应请求: 服务器很容易创建,客户端可以使用浏览器或终端发送HTTP请求,服务器接受请求后返回响应数据。
创建服务器
// 引入http模块
var http = require('http');
// 创建服务,并进行服务监听
http.createServer(function (request, response) {
// 发送 HTTP头部
// HTTP 状态值: 200 : ok
// 内容类型 : text/plain
response.writeHead(200, {'Context-Type':'text/plain'});
// 发送相应数据 "Hello Wrold"
response.end("Hello World\n");
}).listen(8888);
// 终端打印如下信息
console.log("Server running at http:// 127.0.0.1:8888/");
参考网站
- https://www.runoob.com/nodejs/nodejs-tutorial.html
- https://nodejs.org/docs/latest-v13.x/api/documentation.html
本文介绍NodeJS的基础知识,包括如何引入模块,创建服务器并处理HTTP请求。通过一个简单的示例,展示如何创建一个监听特定端口的服务器,接收并响应客户端请求。
182

被折叠的 条评论
为什么被折叠?



