Big front-end learning---Node.js

What is Node.js?

Node.js is actually similar to the jdk that the java program depends on. The helper program runs on the server side, and Node.js allows our JavaScript program to run on the server side, including vue is also dependent on node.js.

One, install Node.js

Click the link: https://nodejs.org/en/ to download directly, open cmd after installation, enter node -v, and the pop-up version number means the installation is successful!

Insert picture description here


It should be noted that after we installed node, npm is also installed by default. npm is a dependency management tool, similar to Maven.

Enter npm -v and the corresponding version number will appear.

Insert picture description here

Two, use Node to run js

//导入http服务
const http=require('http');
//创建http服务
http.createServer(function(request,response){
    //告诉浏览器怎么 解析
    response.writeHead(200,{'Content-type':'text/plain'});

    response.end("hello node server!!!")
}).listen(8888);
console.log("node server服务已经启动!!");
Insert picture description here

Three, use node.js to connect to MySQL

var mysql=require ("mysql")  //1.导入依赖
const { rootCertificates } = require("tls")
//2、创建对象

var connection=mysql.createConnection({
    host:"127.0.0.1",
    port: 3306,
    user: "root",
    password:"root",
    database:"test"
})

//3、开辟连接
connection.connect;
//4、执行连接
connection.query("select * from stud",function(error,results,fields){
    if(error) throw error;
    console.log(results[0]);
})
Insert picture description here