1.想要参考:
去试试其:
https://github.com/roymax/moquery/tree/master
可惜结果安装hiredis就出错了:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js $ npm install redis npm http GET https://registry.npmjs.org/redis npm http 200 https://registry.npmjs.org/redis npm http GET https://registry.npmjs.org/redis/-/redis-0.8.2.tgz npm http 200 https://registry.npmjs.org/redis/-/redis-0.8.2.tgz [email protected] node_modules\redis CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js $ npm install hiredis npm http GET https://registry.npmjs.org/hiredis npm http 200 https://registry.npmjs.org/hiredis npm http GET https://registry.npmjs.org/hiredis/-/hiredis-0.1.14.tgz npm http 200 https://registry.npmjs.org/hiredis/-/hiredis-0.1.14.tgz npm WARN package.json [email protected] No README.md file found! > [email protected] preinstall D:\tmp\tmp_dev_root\node.js\node_modules\hiredis > make || gmake cd deps/hiredis && make static make[1]: Entering directory `/cygdrive/d/tmp/tmp_dev_root/node.js/node_modules/hiredis/deps/hiredis' cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb net.c net.c:1:0: warning: -fPIC ignored for target (all code is position independent) net.c: In function ‘__redisSetErrorFromErrno’: net.c:61:5: warning: implicit declaration of function ‘strerror_r’ cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb hiredis.c hiredis.c:1:0: warning: -fPIC ignored for target (all code is position independent) hiredis.c: In function ‘chrtos’: hiredis.c:226:9: warning: array subscript has type ‘char’ hiredis.c: In function ‘redisvFormatCommand’: hiredis.c:766:21: warning: array subscript has type ‘char’ hiredis.c:771:25: warning: array subscript has type ‘char’ hiredis.c: In function ‘__redisSetError’: hiredis.c:984:9: warning: implicit declaration of function ‘strerror_r’ cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb sds.c sds.c:1:0: warning: -fPIC ignored for target (all code is position independent) sds.c: In function ‘sdstolower’: sds.c:253:5: warning: array subscript has type ‘char’ sds.c: In function ‘sdstoupper’: sds.c:259:5: warning: array subscript has type ‘char’ sds.c: In function ‘sdscatrepr’: sds.c:395:13: warning: array subscript has type ‘char’ sds.c: In function ‘sdssplitargs’: sds.c:427:9: warning: array subscript has type ‘char’ sds.c:455:25: warning: array subscript has type ‘char’ cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb async.c async.c:1:0: warning: -fPIC ignored for target (all code is position independent) async.c: In function ‘__redisGetSubscribeCallback’: async.c:328:9: warning: array subscript has type ‘char’ async.c: In function ‘__redisAsyncCommand’: async.c:537:5: warning: array subscript has type ‘char’ ar rcs libhiredis.a net.o hiredis.o sds.o async.o make[1]: Leaving directory `/cygdrive/d/tmp/tmp_dev_root/node.js/node_modules/hiredis/deps/hiredis' node-waf configure build make: node-waf: Command not found Makefile:2: recipe for target `all' failed make: *** [all] Error 127 'gmake' is not recognized as an internal or external command, operable program or batch file. npm ERR! [email protected] preinstall: `make || gmake` npm ERR! `cmd "/c" "make || gmake"` failed with 1 npm ERR! npm ERR! Failed at the [email protected] preinstall script. npm ERR! This is most likely a problem with the hiredis package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! make || gmake npm ERR! You can get their info via: npm ERR! npm owner ls hiredis npm ERR! There is likely additional logging output above. npm ERR! System Windows_NT 6.1.7601 npm ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "hiredis" npm ERR! cwd D:\tmp\tmp_dev_root\node.js npm ERR! node -v v0.8.14 npm ERR! npm -v 1.1.65 npm ERR! code ELIFECYCLE npm CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js $ npm install brew npm http GET https://registry.npmjs.org/brew npm http 404 https://registry.npmjs.org/brew npm ERR! 404 'brew' is not in the npm registry. npm ERR! 404 You should bug the author to publish it npm ERR! 404 npm ERR! 404 Note that you can also install from a npm ERR! 404 tarball, folder, or http url, or git url. npm ERR! System Windows_NT 6.1.7601 npm ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "brew" npm ERR! cwd D:\tmp\tmp_dev_root\node.js npm ERR! node -v v0.8.14 npm ERR! npm -v 1.1.65 npm ERR! code E404 npm
所以,暂时放弃。
2.参考:
去折腾一下mysql。
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js $ npm install mysql npm http GET https://registry.npmjs.org/mysql npm http 200 https://registry.npmjs.org/mysql npm http GET https://registry.npmjs.org/mysql/-/mysql-2.0.0-alpha4.tgz npm http 200 https://registry.npmjs.org/mysql/-/mysql-2.0.0-alpha4.tgz npm http GET https://registry.npmjs.org/require-all/0.0.3 npm http 200 https://registry.npmjs.org/require-all/0.0.3 npm http GET https://registry.npmjs.org/require-all/-/require-all-0.0.3.tgz npm http 200 https://registry.npmjs.org/require-all/-/require-all-0.0.3.tgz [email protected] node_modules\mysql └── [email protected]
然后拷贝其代码,去做测试,结果出错,现已解决:
3.继续参考:
Getting started with Restify using Node.js and CoffeeScript
去先安装npm install coffee-script。
不过后来也懒得继续折腾了关于coffee-script东西了。
4.最后,使用的,可以运行的js是:
var restify = require('restify'); var mysql = require('mysql'); var server = restify.createServer({ name: 'crifanRestifyServer', version: '1.0.0' }); server.use(restify.acceptParser(server.acceptable)); server.use(restify.queryParser()); server.use(restify.bodyParser()); // server.get('/echo/:name', function (req, res, next) { // res.send(req.params); // return next(); // }); // function respond(req, res, next) { // res.send('hello ' + req.params.name); // } // server.get('/hello/:name', respond); //================================================================================================== // GET /db/table get all database tables // GET /db/table/table_name get the specific database table by name // POST /db/table add a new database table, provide in json format, with name and value //================================================================================================== var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', }); var db_name = "crifan_db"; var table_name = "crifan_db_table"; //create database connection.query('CREATE DATABASE '+ db_name, function(err) { if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) { //throw err; console.log("already exist database:" + db_name); } }); connection.query('USE ' + db_name); function queryTable(req, res, next) { console.log("queryTable be called"); //var queryResult; connection.query( 'SELECT * FROM '+table_name, function selectCb(err, results, fields) { if (err) { throw err; } console.log("now send result"); res.send(results); //console.log(results); //queryResult = results; //console.log(fields); //connection.end(); } ); } server.get('/db', queryTable); function createTable(req, res, next) { console.log("createTable be called"); //console.log(req); //console.log(req.params); var jsonParams = req.params; for(var key in jsonParams) { console.log(key); } //console.log("------------------"); console.log(req.body); //create table // connection.query( // 'CREATE TABLE ' + table_name + // '(id INT(11) AUTO_INCREMENT, '+ // 'name VARCHAR(255), '+ // 'PRIMARY KEY (id))', function(err) { // if (err && err.number != mysql.ER_TABLE_EXISTS_ERROR) { // //throw err; // console.log("already exist table:" + table_name); // } // } // ); connection.query( 'CREATE TABLE ' + table_name + '(name VARCHAR(255), '+ 'PRIMARY KEY (name))', function(err) { if (err && err.number != mysql.ER_TABLE_EXISTS_ERROR) { //throw err; console.log("already exist table:" + table_name); } } ); console.log("create table OK"); var curDate = new Date(); var curMilliSec = curDate.getTime(); connection.query( 'INSERT INTO '+table_name+' '+ 'SET name = ?', ['table_'+curMilliSec] ); // var query = connection.query( // 'INSERT INTO '+table_name+' '+ // 'SET name = ?', // ['nodejs2'] // ); res.send("new table create OK"); return next(); } server.post('/db', createTable); // server.head('/hello/:name', respond); server.listen(2012, function () { console.log('%s listening at %s', server.name, server.url); });
存为server.js
然后通过
node server.js
去运行。
然后客户端,通过curl操作:
Administrator@NYBDHB7EB3XEHP6 /cygdrive/e/Dev_Root/freelance/Elance/projects/GordonHo/node.js/refer/moquery $ curl -i http://localhost:2012/db HTTP/1.1 200 OK Content-Type: application/json Content-Length: 125 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Allow-Methods: GET Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Connection: Keep-Alive Content-MD5: C1NHJd1ttAd946C17hqR5Q== Date: Sat, 10 Nov 2012 14:50:54 GMT Server: crifanRestifyServer X-Api-Version: 1.0.0 X-Request-Id: bcf78f63-ec86-4250-b0dd-c6b80085d5d3 X-Response-Time: 3 [{"name":"nodejs1352558964324"},{"name":"nodejs1352559018528"},{"name":"nodejs1352559022833"},{"name":"table_1352559053268"}] Administrator@NYBDHB7EB3XEHP6 /cygdrive/e/Dev_Root/freelance/Elance/projects/GordonHo/node.js/refer/moquery $ curl -i http://localhost:2012/db -X POST -d '{"tableName":"tbName1"}' -H 'Content-Type:application/json' HTTP/1.1 200 OK Content-Type: application/json Content-Length: 21 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version Access-Control-Allow-Methods: POST Access-Control-Expose-Headers: X-Api-Version, X-Request-Id, X-Response-Time Connection: Keep-Alive Content-MD5: AO45I2kPY4hqZHTBSG0sow== Date: Sat, 10 Nov 2012 14:50:58 GMT Server: crifanRestifyServer X-Api-Version: 1.0.0 X-Request-Id: 9c800e86-e04f-40a0-9076-41dec4a38b65 X-Response-Time: 20 "new table create OK"
对应的,也可以去通过之前安装的MyPhpMyAdmin,查看到对应的mysql中的,的确创建了对于的database和table了。
其中,要注意,第一次是需要先运行下面的带POST的,去创建好对应的数据库,后续才可以通过GET去获得的。
否则先运行GET,由于没有数据库的table,所以肯定会出错的。
转载请注明:在路上 » 【记录】折腾Node.js + mysql + restify