• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

黄文超

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

使用node.js创建简单的服务器

使用node.js创建简单的服务器

使用步骤:
	1、获取http模块
	2、创建一个服务器
	3、服务器监听请求
	4、设置服务器的运行的端口号
// 1、获取http模块
const http = require("http")
// 2、创建一个服务器
const server = http.createServer()
// 3、服务器监听请求
server.on('request',(req,res) => {
	let url = req.url
	let method = req.method
	let str = `这是一个请求,url:${url}和method:${method}。`
	res.setHeader("Content-Type","text/html; charset=utf-8")
	res.end(str)
})
// 4、设置服务器的运行的端口号
server.listen(80,function(){
	console.log("服务器已经启动")
})

posted on 2021-12-11 15:04  黄文超  阅读(80)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3