Loading

Node.js创建一个http服务

 

时间:2022/01/10

 

一.代码

代码如下:

 1 // 导入http模块
 2 const http = require('http');
 3 
 4 // 创建一个httpserver服务
 5 http.createServer(function(require, response){
 6     // 响应头
 7     response.writeHead(200, {'content-type':'text/html'});
 8     // 给浏览器输出的内容
 9     response.end("<strong>hello server!!!</strong>")
10     // 监听的端口号
11 }).listen(8888);
12 
13 console.log("8888端口的服务成功启动!!!")

 

二.服务开启和停止

开始:通过node 文件名来运行js代码。

停止:在控制台中使用control+c

 

posted @ 2022-01-10 20:37    阅读(77)  评论(0编辑  收藏  举报