Node.js快速入门
作者:互联网
1.首先我们看node.js是如何连接游览器的
//导入模块的方式是用require
const http = require('http');
//1.创建一个httpserver服务
http.createServer(function(request,response){
//游览器怎么认识hello server,告诉游览器将以text-plain去解析hello server这段数据
response.writeHead(200,{'Content-type':'text/html'});
//给游览器输出内容
response.end("<strong>hello server!</strong>")
}).listen(8888);
console.log("你已经启动服务是http://localhost:8888");
//2.监听一端口8888
//3.启动运行我们的服务node httpserver.js
//4.在游览器访问http://localhost:8888
a.打开游览器运行的结果,注意这里是8888端口,是我们自己设置的![在这里插入图片描述](https://www.icode9.com/i/ll/?i=56d3e7b54ef84cca848d243f2691b238.png?,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5ZK46bG8X-e_u-i6qw==,size_20,color_FFFFFF,t_70,g_se,x_16)
2.我们看如何连接MySQL数据库的
//导入数据库包,这个要去下载第三方包,
var mysql = require("mysql");
//创建连接
var connection = mysql.createConnection({
host:"127.0.0.1",
user:"root",
port:3306,
password:"admin",
database:"testdb"
});
//连接
connection.connect();
//执行语句
connection.query("select * from wlm",function(error,result,fields){
if(error)throw error;
console.log("results : ",result);
});
//关闭连接
connection.end();
a.安装第三方包MySQL语句npm install mysql
b.数据库中创建的表数据testdb.wlm
c.运行结果
标签:Node,http,入门,游览器,js,connection,mysql,8888,server 来源: https://blog.csdn.net/aaa123_456aaa/article/details/121079978