Nodejs is a server-side language, and it is also a language that must be known on the front-end now.
Simply put, node.js is javascript running on the server. This article intuitively understands node.js by implementing http request responses and connecting to the database for additions, deletions, and changes.
1. Import the http request module
const http = require('http');
This is an API embedded in nodejs, so it can be used directly, just like the import package in python.
2. Create an httpserver service
http.createServer(function(request,response){})
3. Add output content
response.end("<strong>hello server!!</strong>");
This statement should be added to the callback function of the httpserver service
4. Add parsing to the browser
response.writeHead(200,{'Content-type':'text/html'});
Because the browser doesn't know what a hello server is, so if we want the browser to know it, we need to tell the browser to parse the hello server data in text-html, and parse the browser in html to know what the strong tag means. . The same statement should be written in the callback function.
5. Add listening port
Here, combine the first two statements to complete the created httpserver service:
http.createServer(function(request,response) {
response.writeHead(200,{'Content-type':'text/html'})
response.end("<strong>hello server!!</strong>");
}).listen(8888);
Here the port number of our http request is 8888
Finally, let's sort out the complete http request service code:
const http = require('http');
http.createServer(function(request,response) {
response.writeHead(200,{'Content-type':'text/html'})
response.end("<strong>hello server!!</strong>");
}).listen(8888);
console.log("您启动的服务是: http://localhost:8888已启动成功");
6. Start the running service
Run in the integrated terminal:
Seeing this sentence means that our http request service is successful
Type in your browser: http://localhost:8888/qu'fang'wen
You can see that the bolded hello server is output in the browser
Back to step 4, if we change 'text/html' to 'text/plain', that is, text parsing, what will be output in the browser?
Note: After we modify the operation, we need to save the js file, and press Ctrl+c before re-entering it in the terminal.
We restart the service and refresh the browser:
It can be seen that if we tell the browser to parse as text, the strong tag will be output intact. So this is our first introductory case for learning nodejs.
Let's learn how to use node to operate the database
The official documentation of nodejs does not provide us with modules for operating the database, so what should we do? We can import third-party modules
1. Install mysql dependencies
npm install mysql
We can execute this command directly in the terminal of the project project:
Then there is an additional node_modules folder in our project directory:
2. Import the mysql dependency package
var mysql = require("mysql");
3. Configure data connection information
mysql.createConnection({
host:"127.0.0.1",
post:3306,
user:"root",
password:'111',
database:"testdb"
});
Here host is our local ip, post is the database port, user is the username, then the password, and database is the database we created:
After we build the database, we create a new table in it, called user here, and then insert two pieces of data into the table:
4. Create a mysql connection object
We define a connection variable to receive connection information:
var connection = mysql.createConnection({
host:"127.0.0.1",
post:3306,
user:"root",
password:'111',
database:"testdb"
});
5. Open a connection
connection.connect();
6. Execute the statement
connection.query("select * from user",function(error,results,fields) {
//如果查询出错,直接抛出
if(error)throw error;
//查询成功
console.log("results = ",results);
})
Here we directly query all the data in the database, and then output the results result set
7. Close the connection
connection.end();
Put all the pieces together:
var mysql = require("mysql");
var connection = mysql.createConnection({
host:"127.0.0.1",
post:3306,
user:"root",
password:'111',
database:"testdb"
});
connection.connect();
connection.query("select * from user",function(error,results,fields) {
if(error)throw error;
console.log("results = ",results);
})
connection.end();
8. Run and see the results
You can see that all the data we added in the database is output in the terminal
In this way, we can easily implement the database operation through nodejs.