使用node.js 文档里的方法写一个web服务器
刚刚看了node.js文档里的一个小例子,就是用 node.js 写一个web服务器的小例子
上代码 (*^▽^*)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //helloworld.js<br>// 使用node.js写一个服务器 const http=require( 'http' ); const hostname= '127.0.0.1' const port=3000; const server = http.createServer((req,res)=>{ res.statusCode=200; res.setHeader( 'Content-Type' , 'text/plain' ); res.end( 'Hello World\n' ); }); server.listen(port,hostname,()=>{ console.log(`服务器运行在http: //${hostname}:${port}/`); }); |
然后在控制台输入 node helloworld.js ,然后将控制台显示的服务器运行地址复制到浏览器里,如果可以看到 Hello World 就表示服务器运行成功了(^-^)V
感觉这段代码封装的很严实,逻辑很清晰,但是不知道应该怎么用(ಥ_ಥ),以后知道了再来更新吧(*^▽^*)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步