使用Node.js创建HTTP服务器,require指令载入http模块
程序员文章站
2022-09-11 15:15:59
使用require指令来载入http模块,并将其实例化复制给变量http
var http = require("http");
创建服务器,使用htt...
使用require指令来载入http模块,并将其实例化复制给变量http
var http = require("http");创建服务器,使用http.createserver()方法创建服务器,并绑定服务监听端口。函数中可以使用request/response参数来接收和响应数据。代码如下:
var http = require('http'); http.createserver(function(request,response){ //响应头部 response.writehead(200,{'content-type':'text/plain'}); //响应正文数据 response.end('hello world!'); }).listen(8080); console.log('server is running at https://localhost:8080/');
直接运行:
node server.js
这样一个简单的http就创建成功了,非常的方便。
推荐阅读
-
node.js中express模块创建服务器和http模块客户端发请求
-
node.js使用 http-proxy 创建代理服务器操作示例
-
node.js使用http模块创建服务器和客户端完整示例
-
使用Node.js创建HTTP服务器,require指令载入http模块
-
Node.js创建HTTP文件服务器的使用示例
-
【程序】Marvell 88W8686 WiFi模块(WM-G-MR-09)创建或连接热点,并使用lwip2.0.2建立http服务器(20171030版)
-
node.js中express模块创建服务器和http模块客户端发请求
-
node.js使用http模块创建服务器和客户端完整示例
-
node.js使用 http-proxy 创建代理服务器操作示例
-
Node.js —— 使用http模块创建静态web服务器及其路由