Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Node.js function

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use the Node.js function". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Node.js function

In JavaScript, one function can take an argument as another function. We can define a function first and then pass it, or we can define the function directly where the parameters are passed.

The use of functions in Node.js is similar to Javascript. For example, you can do this:

Function say (word) {

Console.log (word)

}

Function execute (someFunction, value) {

SomeFunction (value)

}

Execute (say, "Hello")

In the above code, we pass the say function as the first variable of the execute function. What is returned here is not the return value of say, but the say itself!

In this way, say becomes the local variable someFunction in execute, and execute can use the say function by calling someFunction () (in parentheses).

Of course, because say has a variable, execute can pass such a variable when calling someFunction.

. . .

Anonymous function http://www.iis7.com/a/lm/zzxzgj/

We can pass a function as a variable. But we don't have to go around the circle of "define and then pass". We can define and pass this function directly in parentheses of another function:

Function execute (someFunction, value) {

SomeFunction (value)

}

Execute (function (word) {console.log (word)}, "Hello")

We define the function we are going to pass to execute directly where execute accepts the first argument.

In this way, we don't even have to name this function, which is why it is called an anonymous function.

. . .

How function passing makes the HTTP server work

With this knowledge in mind, let's take a look at our simple but not simple HTTP server:

Var http = require ("http")

Http.createServer (function (request, response) {

Response.writeHead (200,{ "Content-Type": "text/plain"})

Response.write ("Hello World")

Response.end ()

}) .customers (8888)

Now it should look much clearer: we passed an anonymous function to the createServer function.

The same can be achieved with code like this:

Var http = require ("http")

Function onRequest (request, response) {

Response.writeHead (200,{ "Content-Type": "text/plain"})

Response.write ("Hello World")

Response.end ()

}

Http.createServer (onRequest) .clients (8888)

This is the end of the content of "how to use Node.js function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report