I. The basic structure of the Web application of node. js
1. HTTP Server (server.js):
2. Routing (router.js):
For different requests, depending on the URL of the request, the server needs to give different responses, and Word routing is the function used to correspond the request to the request handler (Requesthandle.js).
Routing should also be able to process post data and encapsulate the data in a more friendly format to the request handler, thus requiring the ability to process the requested data
3. Request processing Program (Requesthandle.js):
4. View logic (Requesthandlers.show) ():
Show the content, which means we need some view logic (requesthandlers.show) for the request handler to use to return the picture to the user's browser
5. Upload processing function (requesthandlers.upload):
Users need to upload images, so we need to upload the processing function (Requesthandlers.upload) to deal with this detail.
Ii. description of each structure:
(i) a basic HTTP server
Principle: Put different functions of the code into different modules (different JS source files)
This method allows you to have a clean master file (main files) that you can execute with node. js (App.js or Index.js), and you can have clean modules that can be called by the main file and other modules. It is also easy to scale and maintain.
Now, let's create a master file to launch the app (the main file is called Index.js more or less a standard format) and an HTTP server code module (server.js).
Example:
Server.js
1. Request an HTTP module:
Request (require) node. js's own HTTP module, assigning it to an HTTP variable.
varrequire("http"//请求Node.js自带的 http 模块,把它赋值给http变量
2. Call a method to create a server (Createserver):
http.createServer(function(request, response){}).listen(8888);
Invoke the function provided by the HTTP module: Createserver. This function returns an object (the server object), which has a method called Listen, which has a numeric parameter that specifies the port number that the HTTP server listens on.
We can use this code to start the server and listen on port 8888:
varrequire("http");var server = http.createServer();//定义一个服务器变量/对象server.listen(8888);
This code only initiates a server listening on port 8888, and it does nothing else.
Example:
Server.js
Ask node. js to bring its own HTTP module and assign it to an HTTP? variable.
varrequire("http");http.createServer(function(request,response){ response.writeHead(200,{"Content-Type":"text/plain"});//注意小括号里面的语法 response.write("Hello World"); console.log(“Ingrid”); response.end();//结束语句是必须的}).listen(8888);
Description
Operation Result:
Browser:
Console:
The command box appears two times Ingrid because it accesses two servers, and the URLs for two visits are:
http://localhost:8888/
Http://localhost:8888/server.js
3. Define a function response
function(request, response){}
This function definition is the only parameter of Createserver ()
Description: In JavaScript, functions can be passed in the same way as other variables.
Example:
function say(word) { console.log(word);}function execute(someFunction, value) { "Hello");//把 say 函数作为execute函数的第一个变量进行了传递。//这里传递的不是 say 的返回值,而是 say 本身!//这样一来, say 就变成了 execute 中的本地变量 someFunction
在这个例子中,函数名say作为变量被传递,而且先定义了函数say。我们不一定要“先定义,再传递”,我们可以直接在另一个函数的括号中定定义和传递这个函数,也就是,定义和传递同时进行。如下例:
Example:
Test2.jsfunction execute(someFunction, value) { someFunction(value);}execute(function(word){console.log(word);},"Hello");//跟前一个例子相比,没有定义say函数。//在 execute 接受第一个参数的地方直接定义了我们准备传递给 execute 的函数。//用这种方式,我们甚至不用给这个函数起名字,这也是为什么它被叫做 匿名函数 。
Two
Three
Nodejs Basic Concepts