varHTTP = require (' http ');varMySQL = require (' MySQL ');varConnection =mysql.createconnection ({host:' Rm-bp1w72suk8pc5h6mk.mysql.rds.aliyuncs.com ', User:' r85u40z94p ', Password:' Mafeng11 ', Database:' r85u40z94p '});//start your MySQL connectionConnection.connect ();varServer = Http.createserver (function(req, res) {//If you send a get to http://127.0.0.1:9000/test varUrl_info = require (' URL '). Parse (Req.url,true); //Check if it's for/test's request. if(Url_info.pathname = = = '/test ')) {Res.writehead ($, {' Content-type ': ' Text/plain '}); Connection.query (' SELECT * from ' r85u40z94p '. ' User ' ORDER by rand () LIMIT 5 ',function(err,rows,fields) {//to deal with your results //res.end (rows.constructor); //Output Resultsres.end (json.stringify (rows)); Console.log (Rows.constructor); Console.log (typeof(rows)); Res.end (Rows.join); Console.log (ERR); Console.log (fields); }); } //this is used to reply to the above post, showing the post data to indicate success. If you have a different goal, you naturally don't need this paragraph. Else{req.pipe (res); }}); Server.listen (9000, ' 127.0.0.1 ');//The MySQL connection is also closed when the server shuts downServer.on (' Close ',function() {connection.end ();}); Console.log (' Listening on port 9000 ');
Simple interface implemented by Nodejs