The content of this article is about how to create Http service in Node.js? (Attached is the code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Create WEB server:
/ 创建一个HTTP服务,接收用户的响应 // 引入模块 let http = require("http"); let server = http.createServer(function(req,res) { //req表示请求,res表示响应 //设置响应头 res.writeHead(400,{ "Content-type":"text/html;charset=utf-8" }); //结束响应 res.end("<h1>Hello World,世界!</h1>"); }); // 监听端口 server.listen(4000,"localhost",function () { console.log("Http Server running on port 4000"); });
Can be run in the compiler and opened in the browser: localhost:4000 or http://127.0.0.1:4000/Run it
Related recommendations:
File system file operations in Node.js
The above is the detailed content of How to create Http service in Node.js? (with code). For more information, please follow other related articles on the PHP Chinese website!