C:\Users\horn1\Desktop\node.js\39>node express.js { Error: Handshake inactivity timeout at Handshake.<anonymous> (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\Protocol.js:164:17) at emitNone (events.js:105:13) at Handshake.emit (events.js:207:7) at Handshake._onTimeout (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\sequences\Sequence.js:129:8) at ontimeout (timers.js:469:11) at tryOnTimeout (timers.js:304:5) at Timer.listOnTimeout (timers.js:264:5) -------------------- at Protocol._enqueue (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\Protocol.js:145:48) at Protocol.handshake (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\protocol\Protocol.js:52:23) at PoolConnection.connect (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\Connection.js:130:18) at Pool.getConnection (C:\Users\horn1\Desktop\node.js\39\node_modules\mysql\lib\Pool.js:48:16) at IncomingMessage.<anonymous> (C:\Users\horn1\Desktop\node.js\39\express.js:29:8) at emitOne (events.js:115:13) at IncomingMessage.emit (events.js:210:7) at IncomingMessage.Readable.read (_stream_readable.js:478:10) at flow (_stream_readable.js:849:34) at resume_ (_stream_readable.js:831:3) code: ‘PROTOCOL_SEQUENCE_TIMEOUT‘, fatal: true, timeout: 10000 }
代码如下:
‘use strict‘; var express=require(‘express‘); var fs=require(‘fs‘); var querystring=require(‘querystring‘); var http=require(‘http‘); var app=express(); var mysql=require(‘mysql‘); var pool=mysql.createPool({ host:‘192.168.80.129‘,// 数据库在本地虚拟机上,能用mysql-front连通 port:‘3306‘, database:‘test‘, user:‘root‘, password:‘12345678‘, timeout:1000000000, connectTimeout:1000000000, }); app.get(‘/index.html‘,function(req,res){ res.writeHead(200,{‘Content-type‘:‘text/html‘}); var file=fs.createReadStream(‘index.html‘); file.pipe(res); }); app.post(‘/index.html‘,function(req,res){ req.on(‘data‘,function(data){ var obj=querystring.parse(data.toString()); pool.getConnection(function(err,connection){ if(err){ res.send(‘Can not connect to MySql DB‘); console.log(err); }else{ var str; connection.query(‘insert into emp set ?‘,{name:obj.name},function(err,result){ if(err){ str=‘Insert failed‘; }else{ str="Insert succeed"; } connection.release(); res.send(str); }) } }); }); }); app.listen(3333,"127.0.0.1");