node连接数据库
//1、引入
const mysql = require( "mysql" ) ;
//2、创建连接对象
//使用createConnection( ) 方法来创建数据库参数
//数据库参数的形式:
//1、对象的形式(推荐使用)
const conn = mysql.createConnection( {
host:"10.10.32.162" ,//数据库地址,如果是本地使用localhost
port:3306,//数据库的端口号,默认3306
user:"web0817" ,//数据库的用户名
password:"123456" ,//数据库的密码
database:"xiao_u_2" //数据库的名字
//charset:连接的编码方式
//connectTimeout:连接超时时间,默认为10000 毫秒
} ) ;
//2、字符串的形式
//字符串形式的书写格式
//数据库类型://用户名:密码@数据库的地址/数据库的名字
const connstr= ` mysql://web0817:123456@10.10.32.162/xiao_u_2`
const conn = mysql.createConnection( connstr)
//3、建立连接
// connect( ) 此方法用来建立数据库连接
//方式一:不带回调函数的
conn.connect( ) ;
//方式二:带回调函数的( 推荐使用)
conn.connect(( err) = > {
if( err) console.log( err) ;
} )
//执行一条sql语句,测试数据库是否连接成功
conn.query( ` select * from users ` ,( err,data) = > {
console.log( data) ;
} ) ;
//console.log( conn) ;
//4、关闭连接
//方式一:不带回调函数
conn.end( ) ;
//方式二:带回调函数的( 推荐使用)
conn.end(( err) = > {
if( err) console.log( err) ;
} ) ;
//方式三:销毁模式
conn.destroy( ) ;
//面试题请问关闭数据的方法中end( ) 与destroy( ) :end可以接回调函数,destroy不能接回调函数。
数据库语句的执行
module.exports= {
host:"10.10.32.162" ,
user:"web0817" ,
password:"123456" ,
port:3306,
database:"xiao_u_2"
} ;
//select返回数组,其他返回对象
//1、引入
const mysql= require( "mysql" ) ;
const dbconfig= require( "./db_config" ) ;
//2、创建对象
const conn= mysql.createConnection( dbconfig) ;
//3、建立连接
conn.connect(( err) = > {
if( err) console.log( err) ;
} ) ;
//执行sql语句,在Node执行sql语句采用的方法query( ) ,可以所有的sql语句,但执行查询语句的时候,返回结果是一个数组(注:即使是一条语句也是数组的形式)
//参数一 ,需要执行的sql语句
//参数二,以数组的形式给SQL语句来传递参数,注:即使一个参数也是数组的形式,此方法为了防止sql注入式攻击。且参数与问号一定要一一对应
//参数三,回调函数,可以拿到执行的错误信息及返回值
//删除指定数据
conn.query( ` delete from users where username= ?` ,[ "" ] ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} )
//清除表中数据
conn.query( ` delete from banner` ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} ) ;
//删除数据库:删库需慎重!!!!
conn.query( ` drop database test ` ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} )
//带条件的更新语句//affectedRows,指被更改的条数
conn.query( ` update users set pwd= ? where username= ?` ,[ '123456' ,"" ] ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} )
//更新语句
conn.query( ` update users set pwd= 'e10adc3949ba59abbe56e057f20f883e' ` ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} ) ;
//新增数据
//做新增插入的时候要排除"" ,"null" ,"undefined"
conn.query( ` insert users( username,realname) value( ?,?) ` ,[ "" ,'小新' ] ,( err,data) = > {
if( err) {
if( err.message.includes( "Duplicate entry" )) {
console.log( "该用户数据库已存在!" ) ;
} else if( err.message.includes( "be null" )) {
console.log( "用户名不能为空" ) ;
} else{
console.log( err) ;
}
}
else{
console.log( data) ;
}
} )
/* 采用sql传参的形式,此形式采用?占位符,来接受传递的参数*/
conn.query( ` select * from users where username= ? and pwd= ? ` ,[ 'admin' ,'e10adc3949ba59abbe56e057f20f883e' ] ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} ) ;
/* 执行条件查询 */
conn.query( ` select * from users where username= 'admin' and pwd= 'e10adc3949ba59abbe56e057f20f883e' ` ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} ) ;
/* 执行正常的查询语句 */
conn.query( ` select * from users ` ,( err,data) = > {
if( err) console.log( err) ;
else console.log( data) ;
} ) ;
//4、关闭连接
conn.end(( err) = > {
if( err) console.log( err) ;
} )
webpack
是一个现代 JavaScript 应用程序的静态模块打包工具 (module bundler)。当 webpack 处理应用程序时,它会在内部构建一个 依赖图(dependency graph),此依赖图对应映射到项目所需的每个模块,并生成一个或多个 bundle。目前很多框架的脚手架都是基于webpack,如vue、reactjs、angular等。
安装:npm i webpack webpack-cli -D
webpack-cli,用于在命令行中运行 webpack
网址
在webpack中,所有的静态资源都可以被处理为一个模块,包括js、图片、css、字体、HTML。
注:webpack4.0开始,可以不再需要配置文件来打包js文件。只需要按路径配置好参数即可。
打包过程如下: Webpack常见的概念,如下:
Entry ,(入口),工程的入口 文件配置
Output ,(出口),打包出口 的文件配置
Loader ,(加载器 ),将所有类型的文件转换为 webpack 能够处理的有效模块,可扩展
Plugins ,(插件 ) ,在webpack打包过程中不同时机执行一些任务,比如清除打包目录、复制静态文件、抽取css文件等,从打包优化和压缩,一直到重新定义环境中的变量
Mode ,(模式 ),开发环境和生成(产品)环境
注:新版的webpack打包默认js时,可以不需要配置webpack.config.js文件,直接可以进行打包,但文件入口路径是默认值路径,文件出口路径是默认值输出。
entry入口
entry ,工程的入口文件,通过配置 entry 属性ÿ