Home > Web Front-end > JS Tutorial > body text

How to create Http service in Node.js? (with code)

不言
Release: 2018-08-23 17:43:36
Original
1348 people have browsed it

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");
});
Copy after login

Can be run in the compiler and opened in the browser: localhost:4000 or http://127.0.0.1:4000/Run it

Related recommendations:

Introduction to the Node.js module system and how to load the module Code implementation to implement

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!