nodejs实现的简单接口
生活随笔
收集整理的這篇文章主要介紹了
nodejs实现的简单接口
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
var http = require('http');
var mysql = require('mysql');
var connection = mysql.createConnection({host : 'rm-bp1w72suk8pc5h6mk.mysql.rds.aliyuncs.com',user : 'r85u40z94p',password : 'Mafeng11',database: 'r85u40z94p'
});
//開始你的mysql連接
connection.connect();var server = http.createServer(function (req, res) {//如果你發(fā)一個(gè)GET到http://127.0.0.1:9000/testvar url_info = require('url').parse(req.url, true);//檢查是不是給/test的requestif(url_info.pathname === '/test'){res.writeHead(200, {'Content-Type': 'text/plain'});connection.query('SELECT * FROM `r85u40z94p`.`user` order by rand() LIMIT 5',function(err,rows,fields){//處理你的結(jié)果// res.end(rows.constructor);// 輸出結(jié)果
res.end(JSON.stringify(rows));console.log(rows.constructor);console.log(typeof(rows));res.end(rows.join);console.log(err);console.log(fields);});}//這個(gè)是用來回復(fù)上面那個(gè)post的,顯示post的數(shù)據(jù)以表示成功了。你要是有別的目標(biāo),自然不需要這一段。else{req.pipe(res);}
});
server.listen(9000, '127.0.0.1');
//在server關(guān)閉的時(shí)候也關(guān)閉mysql連接
server.on('close',function(){connection.end();
});
console.log('listening on port 9000');
?
轉(zhuǎn)載于:https://www.cnblogs.com/mafeng/p/6106893.html
總結(jié)
以上是生活随笔為你收集整理的nodejs实现的简单接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GridView不能添加头布局,并且sc
- 下一篇: SqlHelper简单实现(通过Expr