1. NodeJs installation
You can visit the NodeJs website to download the latest installation package and install it directly.
NodeJs official website: https://nodejs.org/en/
NodeJs: http://nodejs.cn/
This side contains the latest version and stable version, you can choose to install.
After the installation is complete, enter the command
> node -v
Copy the code
or
> npm -v
Copy the code
Check whether the command is available. If the output version is normal, the installation is ok.
2. Common NPM and node commands
NPM common commands
> Output the current version of NPM
> npm -v
> Upgrade the current NPM
> npm install -g npm
> Install partial modules
> npm install
> Install the global module
> npm install -g xxx
> Run the script command
> npm run xxx
> # Display all command information
> npm -l
> ...
Copy the code
Common Node Commands
> Execute the js file
> node xxx.js
Copy the code
3, in Nodejshttp
The module
Development recommended direct useVSCode
Develop. You are advised to install plug-in modulesnode-snippets
.So let’s just create onejs
File. The inputnode
After that, it will automatically prompt youhttp-http-server
, select Enter, you can see the following quick creationhttp
Request code. 😄, there is no comment provided by default, I have added a comment here for ease of understanding.
// Introduce the HTTP module
var http = require('http');
/** * request: the server requests information, such as parameters input from the user */ Response: the server responds to the information, which can be set back to the user */
http.createServer(function (request, response) {
/**
* 设置请求头信息
* code(statusCode): 200
* headers信息: Content-Type
*/
response.writeHead(200, {'Content-Type': 'text/plain; charset=utf8'});
// Set the message to be returned
response.write('Hello World');
// Tell the client that the request is over
response.end();
})
// Request listening on port 8081
.listen(8081);
console.log('Server running at http://127.0.0.1:8081/);
Copy the code
Run the code
> node .\nodejs_http.js
Copy the code
After running, you can see the log output
Server running at http:/ / 127.0.0.1:8081 /
Copy the code
Direct browser access, you can see the outputThe above is just a very simple request return, in real use, must be passed parameters, and then returned. Let’s look at how to pass and receive parameters.
Or the example above, the simplest direct GET request, concatenated with one parameter? The username = zhangsan&age = 22.
http://127.0.0.1:8081/?username=zhangsan&age=22
Copy the code
As mentioned earlier, the request parameter is used to fetch parameters from the client, so let’s print it out first.
console.log(request.url)
// Log output/? username=zhangsan&age=21
Copy the code
You can see that the request parameters are output normally. The following is to parse to get the parameter, parameter parsing requires the user another module url.
4, nodejsurl
The module
First, introduce the URL module
// Place it directly above the import HTTP module
var url = require('url');
Copy the code
Parse (request. Url) by adding a line to request
console.log(url.parse(request.url, true)) ParseQueryString =true
// Parse queryString into objects.
Copy the code
Let’s take a look at the print, focusing on queryString.
Url {
protocol: null.slashes: null.auth: null.host: null.port: null.hostname: null.hash: null.search: '? username=zhangsan&age=21'.//*** focus on here
query: [Object: null prototype] { username: 'zhangsan'.age: '21' },
pathname: '/'.path: '/? username=zhangsan&age=21'.href: '/? username=zhangsan&age=21'
}
Copy the code
This time, we will parse the received username and age from the request and return them to the client in the form of JSON. At present, the design is to separate the front end from the back end, so it is common to directly return an interface form, so we simulate the return interface form here.
// Since a normal request is requested twice, once is a normal request and once is favicon.icon, this is filtered out
if(request.url ! ='/favicon.ico') {// Use url.parse to parse the requested queryString, set to true,
// Translates queryString directly to a JSON object
let queryUrl = url.parse(request.url, true).query
// Construct a JSON return value
let responseResult = {
code: 0.message: 'Request successful'.data: {
username: queryUrl.username,
age: queryUrl.age
}
}
// Set the return information, where the result is returned to the client as json
response.write(JSON.stringify(responseResult));
}
Copy the code
Refresh the browser again and look at the browser output
At this point, a simple request and return data operation is complete.