Node.js learning to build a simple Node server

Check my node version

node -v

Here is Node 14.15.1 version

Generally, after installing node, we will automatically install npm for us and check the version of npm

npm -v

Npm is the package manager of node, similar to pip in python. When we use node, when we need to install other packages, we can use npm to install

Let's look at the following code first, how do we start a node service.

// 导入nodejs http模块包
var http = require('http');

//通过createServer,创建一个服务,内置一个回调函数,接收两个参数,一个是http请求request
// 一个是对这个请求的响应,也就是说,当这个服务创建完成之后就会去请求这个回调函数
var server = http.createServer(function (request, response) {
    //设置http响应信息
    response.writeHead(200, {'Content-Type': 'text/plain'});
    //发送字符串到响应客户端
    response.end("Hello Node.js")
});

//我们需要对某个端口号进行监听
server.listen(3000, 'localhost');
//打印启动日志
console.log("Node server started on prot 3000")

The above is a nodejs server that we reduced orders in node.

After we are up and running, we can see the printed log content in the console

Then we use the browser to visit  http://localhost:3000/

Get the value we passed from the server to the page.

It is explained here that a simple node server we wrote is completed.

That's all for today. Thank you for reading and. Thank you.

 

Guess you like

Origin blog.csdn.net/u012798683/article/details/109902395