Node学习笔记(二)—— 建立简单服务器

  1. 创建server.js文件
    // 1. 导入http模块
    let http = require('http')
    // 2. 创建服务器并返回数据hello world
    http.createServer(function(request,response){
    	// 发送 HTTP 头部 
        // HTTP 状态值: 200 : OK
        // 内容类型: text/plain
        response.writeHead(200,{'Content-type':"text/plain"})
        // 发送响应数据 “hello world”
        response.end('hello world')
    }).listen(8888) // 设置端口号为 8888
    
  2. 开启终端,cdserver.js文件所在目录
  3. 运行node server.js 命令,通过浏览器访问http://127.0.0.1:8888/就可以看见页面中输入了hello world
发布了122 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44876003/article/details/104767974