node.js database operations


node using MySQL const HTTP
= the require ( 'HTTP' ); const MySQL = the require ( 'MySQL' ); const URL = the require ( 'URL' ); const FS = the require ( 'FS' ); // 1. connection to database the let DB = mysql.createConnection ({ Host: 'localhost' , User: 'the root' , password: '' , database: '' }); // connection pool resolve concurrency [problem] / * the let DB = mysql.createPool ({ connerctionLimit:10, // default is 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') { //Parameters are correct the let {username, password} = Query; IF (username ||!! Password) { res.write ( 'user and password can not be empty' ); res.end (); } the else IF (username.length> 32 ) { res.write ( 'user name can not be greater than 32' ); res.end (); } the else IF (password.length> 32 ) { res.write ( 'the password is not greater than 32' ); res.end (); } the 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=>{ the elseIF (ERR) { res.write ( 'wrong database' ); res.end (); } the else { res.write ( 'registration success' ); res.end (); } }) } }) } // check username exists } the else iF (pathname === '/ Login' ) { } { 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: 'the root' , password: '' , Database: '' }); the let DB = CO.'s (Conn); http.createServer (the async (REQ, RES) => { const {pathname, Query} = url.parse ( req.url, to true ); iF (pathname === '/ REG' ) { // parameters are correct the let {username, password} = Query; iF !! (|| username password) { res.write ( 'users and password can not be empty ' res.end (); } The else IF (username.length> 32 ) { res.write ( 'User name can not be greater than 32' ); res.end (); } the else IF (password.length> 32 ) { res.write ( 'password can not be greater than 32 ' ); res.end (); } the else { the try { the let Data = db.query the await (the FROM `the SELECT ID user_table the WHERE username =' $ {username} ' `); IF (data.length> 0 ) { res.write ( 'User name already exists' ); } the else { the await db.query ( `the INFO user_table the SELECT (username, password) the VALUES ( '$ {username}', '{$ password}' )`); RES. Write ( 'registration success' ); } } the catch (ERR) { res.write ( ' wrong database ' ); } res.end (); } // check the user name exists } the 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(); }) } })

 

Guess you like

Origin www.cnblogs.com/jjq-exchange/p/11562997.html