node 中使用mysql
const http = require('http');
const mysql = require('mysql');
const url = require('url');
const fs = require('fs'); //1.连接到数据库
let db = mysql.createConnection({
host:'localhost',
user: 'root',
password:'',
database:''
}); //连接池【解决并发问题】
/*let db = mysql.createPool({
connerctionLimit: 10,//默认就是10
host:'localhost',
user: 'root',
password:'',
database:''
});*/ //查询
db.query('SELECT * FORM user_Table',(err,data)=>{
if(err){
console.log('错了');
}else{
console.log(data);
}
}); //2.配合http
http.createServer((req,res)=>{
const {pathname,query} = url.parse(req.url,true); if(pathname === '/reg'){
//参数是否正确
let {username,password} = query;
if(!username || !password) {
res.write('用户和密码不能为空');
res.end();
}else if(username.length>32){
res.write('用户名不能大于32位');
res.end();
}else if(password.length>32){
res.write('密码不能大于32位');
res.end();
}else{
db.query(`SELECT ID FROM user_table WHERE username='${username}'`,(err,data)=>{
if(err){
res.write('数据库有错');
res.end();
}else if(data.length>0){
res.write('用户名已存在');
res.end();
}else{
db.query(`SELECT INFO user_table (username,password) VALUES('${username}','${password}')`,err=>{
if(err){
res.write('数据库有错');
res.end();
}else {
res.write('注册成功');
res.end();
}
})
}
})
} //检查用户名是否存在 }else if(pathname === '/login'){ }else{
fs.readFile('www'+pathname,(err,buffer)=>{
if(err){
res.writeHeader(404);
res.write('no found');
}else{
res.write(buffer);
}
res.end();
})
}
}) 3.异步模块(co-mysql)
const co = require('co-mysql');
let conn = mysql.createPool({
host:'localhost',
user: 'root',
password:'',
database:''
}); let db = co(conn); http.createServer(async (req,res)=>{
const {pathname,query} = url.parse(req.url,true); if(pathname === '/reg'){
//参数是否正确
let {username,password} = query;
if(!username || !password) {
res.write('用户和密码不能为空');
res.end();
}else if(username.length>32){
res.write('用户名不能大于32位');
res.end();
}else if(password.length>32){
res.write('密码不能大于32位');
res.end();
}else{
try{
let data = await db.query(`SELECT ID FROM user_table WHERE username='${username}'`);
if(data.length>0){
res.write('用户名已存在');
}else{
await db.query(`SELECT INFO user_table (username,password) VALUES('${username}','${password}')`);
res.write('注册成功');
}
}catch(err){
res.write('数据库有错');
}
res.end();
} //检查用户名是否存在 }else if(pathname === '/login'){ }else{
fs.readFile('www'+pathname,(err,buffer)=>{
if(err){
res.writeHeader(404);
res.write('no found');
}else{
res.write(buffer);
}
res.end();
})
}
})

node.js数据库操作的更多相关文章

  1. node.js高效操作mongodb

    node.js高效操作mongodb Mongoose库简而言之就是在node环境中操作MongoDB数据库的一种便捷的封装,一种对象模型工具,类似ORM,Mongoose将数据库中的数据转换为Jav ...

  2. Node.js 数据库实时监控库 node-dbmon

    node-dbmon 是一个 Node.js 数据库实时监控库,如果你希望在数据库表数据更改后,或者是文件修改后能更新 GUI,那么这个库正好适合你. https://github.com/strap ...

  3. Node.js之操作文件系统(一)

    Node.js之操作文件系统(一) 1. 同步方法与异步方法 在Node.js中,使用fs模块来实现所有有关文件及目录的创建.写入及删除操作.,在fs模块中,所有对文件及目录的操作都可以使用同步与异步 ...

  4. Node.js之操作文件系统(二)

    Node.js之操作文件系统(二) 1.创建与读取目录 1.1 创建目录 在fs模块中,可以使用mkdir方法创建目录,该方法的使用方法如下: fs.mkdir(path,[mode],callbca ...

  5. 在Node.js中操作文件系统(一)

    在Node.js中操作文件系统 在Node.js中,使用fs模块来实现所有有关文件及目录的创建,写入及删除操作.在fs模块中,所有对文件及目录的操作都可以使用同步与异步这两种方法.比如在执行读文件操作 ...

  6. Node.js文件操作二

    前面的博客 Node.js文件操作一中主要是对文件的读写操作,其实还有文件这块还有一些其他操作. 一.验证文件path是否正确(系统是如下定义的) fs.exists = function(path, ...

  7. mongoose - 让node.js高效操作mongodb

    Mongoose库简而言之就是在node环境中操作MongoDB数据库的一种便捷的封装,一种对象模型工具,类似ORM,Mongoose将数据库中的数据转换为JavaScript对象以供你在应用中使用. ...

  8. MONGOOSE – 让NODE.JS高效操作MONGODB(转载)

    Mongoose库简而言之就是在node环境中操作MongoDB数据库的一种便捷的封装,一种对象模型工具,类似ORM,Mongoose将数据库中的数据转换为JavaScript对象以供你在应用中使用. ...

  9. Node.js简单操作

    在node中是不支持BOM和DOM操作的,所以像alert().document.write...都是不支持的,可以是console.log() 首先我们来输出"hello world&qu ...

随机推荐

  1. pymysql装饰器封装

    pymysql装饰器封装 def openClose(fun): def run(sql=None): coon =pymysql.connect(host='localhost' ,port=330 ...

  2. Cookie的Secure属性和HttpOnly属性

    基于安全的考虑,需要给cookie加上Secure和HttpOnly属性,HttpOnly比较好理解,设置HttpOnly=true的cookie不能被js获取到,无法用document.cookie ...

  3. mybatis ResultHandler vs ResultSetHandler及自定义扩展

    ResultSetHandler是mybatis的关键类之一,用于对jdbc返回的ResultSet进行映射处理,其中包括列前缀处理,逻辑分页,鉴别器(Discriminator,基于值实现动态映射列 ...

  4. sql server中raiserror的用法(动态参数传值)

    1.raiserrror定义: 返回用户定义的错误信息并设系统标志,记录发生错误.通过使用 RAISERROR 语句,客户端可以从 sysmessages 表中检索条目, 或者使用用户指定的严重度和状 ...

  5. EVE-NG使用手册

    转裁于https://www.cnblogs.com/51yuki/articles/eve01.html EVE-NG使用手册   一)EVE-NG的安装 1)下载EVE镜像包 https://pa ...

  6. FileHelper-文件操作工具

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  7. Nodejs接收图片base64格式保存为文件

    base64的形式为“data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0....”:当接收到上边的内容后,需要将data ...

  8. C# .net 提升 asp.net mvc, asp.net core mvc 并发量

    1.提升System.Net.ServicePointManager.DefaultConnectionLimit 2.提升最小工作线程数 ------ DefaultConnectionLimit在 ...

  9. 【并行计算-CUDA开发】GPU并行编程方法

    转载自:http://blog.sina.com.cn/s/blog_a43b3cf2010157ph.html 编写利用GPU加速的并行程序有多种方法,归纳起来有三种: 1.      利用现有的G ...

  10. spring security实现记录用户登录时间等信息

    目录 spring security实现记录用户登录时间等信息 一.原理分析 二.实现方式 2.1 自定义AuthenticationSuccessHandler实现类 2.2 在spring-sec ...