当前位置:网站首页>用Node创建一个服务器
用Node创建一个服务器
2022-06-21 16:04:00 【玉安_ZhangDe】
1、导入 http 模块
// 1、导入http模块
const http = require('http')
2、创建 web 服务器实例
// 2、创建web服务器实例
const server = http.createServer()
3、为服务器实例绑定 request 事件,监听客户端的请求
// 3、为服务器绑定request事件
server.on('request',(req,res)=>{
// req.url 获取请求地址 默认是/
const url = req.url
console.log(url);
// req.method 获取请求的方式 get/post
const method = req.method
console.log(method);
// res.end() 服务器向客户端响应内容 暂不支持中文
res.end('Hello world!')
})4、 启动服务器
// 4 、启动服务器
server.listen(80,()=>{
console.log('server running at http://localhost');
})运行结果示意

安装配置NodeJs环境
如若想实现上述功能,首先需要安装配置NodeJs环境,我把链接放在这里,请前往菜鸟教程去看具体的教程,这里我就不多做赘述了。
边栏推荐
- 2022年第三届全国运筹学/数据、模型与决策课程教学研讨会通知
- data type
- 【Leetcode】297. Serialization and deserialization of binary tree (difficult)
- UDP和TCP的对比
- rtmp webrtc 协议 openssl 等安装
- win32com 操作excel
- Postman basic operations
- Hairui technology completed the pre-A round of financing of tens of millions of yuan to build the first artificial intelligent distribution Internet of things in China
- Comparison of UDP and TCP
- PowerPoint 教程,如何在 PowerPoint 中更改页面方向、幻灯片大小?
猜你喜欢

垃圾回收器
![[MySQL learning notes 18] constraints](/img/29/c72f83bfae8fd8b43e78cdf1aa9cbc.png)
[MySQL learning notes 18] constraints

很多軟件公司,其實都是“笑話”

Four areas of telephone memory

智能制造的下一站:云原生+边缘计算双轮驱动

PowerPoint tutorial, how to change page orientation and slide size in PowerPoint?

Alibaba cloud server + pagoda panel + no domain name deployment web project

Pytest framework implements pre post processing
![[MySQL learning notes 15] user management](/img/e8/4773f99c42891789b0311506b8384f.png)
[MySQL learning notes 15] user management

QT knowledge: using the qgraphicspixmapitem class
随机推荐
建立自己的网站(11)
【mysql学习笔记18】约束
中国游戏的“外卷”大时代,中小厂商如何破解出海难题?
Move Protocol Beta测试版稳定,临时决定奖池规模再扩大
Force deduction solution summary 1108-ip address invalidation
Uni app framework learning notes
Design and implementation of face verification system for floating population management
自然科学的根本任务
[MySQL learning notes 11] sort query
【mysql学习笔记12】分页查询
[Error] ‘vector‘ was not declared in this scope
Comparison of UDP and TCP
Common formula of derivative__ Common formulas of indefinite integral
第13周总结博客(校历第15周)动态规划总结
Qt5 knowledge: string list qstringlistmodel
Leetcode 25: a group of K flipped linked lists
Sequence traversal of binary tree
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
Fisher信息量检测对抗样本代码详解
数据类型