Node.JS 】http 的概念及作用
什么是 http 模块
在网络节点中,负责消费资源的电脑,叫做客户端,负责对外提供网络资源的电脑,叫做服务器。
http 模块是 Node.js 官方提供的,用来创建 web 服务器的模块,通过 http 模块提供的 http.createServer()方法,就能方便的把一台普通的电脑,编程一台 web 服务器,从而对外提供 Web 资源服务。
使用时同样需要导入相关模块。
复制代码

点击并拖拽以移动
http 模块的作用
服务器和普通电脑的区别在于,服务器上安装了 web 服务器软件,例如:lls Apache 等。通过安装这些服务器软件,就能把一台普通的电脑变成一台 web 服务器。
Apache


点击并拖拽以移动
编辑
在其他选项菜单中 打开根目录文件,在里面放入你的网页文件


点击并拖拽以移动
编辑
网址中输入你的 ip 127.0.0.1 就可以了。
不过这些东西 都不是我们前端程序员用的,在我们 Node.js 中,我们不需要使用 lls,Apache 等这些第三方 web 服务器软件,因为我们可以基于 Node.js 提供的 http 模块,通过几行简单的代码,就能轻松的手写一个服务器软件,从而对外提供 web 服务。
注意
每个端口号不能同时被多个 web 服务占用
在实际应用中,url 中的 80 端口可以省略
评论