‘use strict‘; //加载http模块; const http = require(‘http‘); //创建一个http服务; const server = http.createServer((req, res) => { res.writeHead(200, { "content-type": ‘text/html‘ }); res.write(‘<h2>这是一个http服务</h2>‘); res.end(); }); server.listen(3100, () => { console.log(‘server is running at 3100‘); })
js是基于事件的语言,这个代码本质如下
‘use strict’; //引入http模块 const http = require(‘http‘); //创建server对象 const server = http.creatServer(); //监听端口 server.listen(3000,()=>{ console.log(‘http server is listening at poat 3000‘); }); //此时就已经监听了。相当于餐馆开业但是没有厨师; // 需要注册一个事件; 相当于弄来一个厨师; server.on(‘request‘,function(req,res){ res.writeHead(200,{‘content-type‘:‘text/html;charset=utf-8‘}); res.write(‘<h2>响应<h2>‘); res.end(); })
时间: 2024-10-04 13:05:11