1. Introduction
Node.js is a JavaScript runtime environment (runtime) based on the Chrome V8 engine. It
uses an event-driven non-blocking I / O model to make it lightweight and efficient.
It enables JavaScript to develop back-end programs to implement almost other back-end languages. All functions that can be achieved
2. Installation and preparation
Official website to download and install the package: https://nodejs.org/en/
Go directly to the next step to install a fool
After installation, enter in cmd node -v
to view the current node version
Smart tips for VScode:
Enter in cmd:
npm install --save-dev @types/node
Download typings. After downloading, restart VScode to use
npm. It is a package manager. As long as there are NodeJs, npm can use it to download some packages and related tools.
3. Use
1. Http module
If you use PHP to write the back-end code, you need an Apache or Nginx server to process the client's request response.
If you use Java to write the back-end code, you need the Tomcat or Nginx server to process the client's request response.
But the concept is completely different for Node.js for
use in achieving a just Node.js application also implements the entire HTTP server
// 引入http模块
var http=require("http");
// 用http模块创建服务
/**
* req 获取url信息(request)
* resp 浏览器返回信息(response)
*/
http.createServer(function(req,resp){
// 发送Http头部 状态值200 文件类型html 字符集utf-8
resp.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
// 发送响应数据
resp.write("NodeJs Hello World!");
// 结束响应
resp.end();
}).listen(8001); // 设置监听端口
In cmd, enter the drive letter where the Nodejs file is located node 文件名
to run
successfully after startup: the
request header is also set by yourself:
so simple to achieve the access to the NodeJs server
2. Url module
The Url module has three methods: parse () and format () and resolve ()
①、parse()
The parse method is mainly used to parse the url and is the most commonly used method
in the Url module. You can pass the url address in parse () to get the request information: When
the second parameter of the parse () method is true, it means that the request information is parsed and converted into Object:
②、format()
Can convert the object to the requested address and parse () is the opposite
③、resolve()
The request url can be replaced:
3. Print url
var http=require("http");
http.createServer(function(req,resp){
// 打印加载的url
console.log(req.url);
resp.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
resp.write("NodeJs Hello World!");
resp.end();
}).listen(8001);
Example 1:
Access path:
Output:
Example 2:
Access path:
Output:
Example 3:
Access path:
Output:
This /favicon.ico is the small icon on the left side of the browser label that will automatically load with the page every time,
such as "write an article" The red "C" on the left is a favicon.ico
4. Get request parameters
var http=require("http");
var url=require("url")
http.createServer(function(req,resp){
resp.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
if (req.url!="/favicon.ico")
{
// req.url:获取浏览器url输入的信息
console.log(req.url);
// 解析url parse()第一个参数为地址 第二个参数传入true 会将请求的参数转换为对象
var result=url.parse(req.url,true);
console.log(result);
}
resp.write("NodeJs Hello World!");
resp.end();
}).listen(8001);
After getting a lot of data,
you can also get its specified attributes
5. Get the specified parameters of the url's get pass value
var http=require("http");
var url=require("url")
http.createServer(function(req,resp){
resp.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
// 不解析图标图片
if (req.url!="/favicon.ico")
{
// 解析url parse()第一个参数为地址 第二个参数传入true 会将请求的参数转换为对象
var result=url.parse(req.url,true);
// 获取url的id值
console.log(result.query.id);
}
resp.write("NodeJs Hello World!");
resp.end();
}).listen(8001);
When accessing HTTP: // localhost: 8001 / News the above mentioned id =? 1 time
Output:
Successfully obtain the specified parameters
Four, Nodejs self-starting tool supervisor
The supervisor will constantly monitor all the files under the application.
Once it is found that the file has been modified, it will reload the program file. This realizes the deployment.
After the program file is modified, you can see the changed result immediately. There is
no need to restart nodejs every time.
installation:
Enter under cmd:
npm install -g supervisor
use
Use the supervisor
command instead of the node command to start the application.
Each time the code is saved, the server will automatically restart: