node.js - NodeJS ExpressJS Restfull API -
i'm trying implement webservices nodejs , expressjs.
this code :
node.js , expressjs:
var express = require('express'); var routes = require('./routes'); var skybiometry = require('./routes/skybiometry'); var http = require('http'); var path = require('path'); var app = express(); app.set('port', process.env.port || 3000); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodoverride()); app.use(app.router); app.use(require('stylus').middleware(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public'))); // development if ('development' == app.get('env')) { app.use(express.errorhandler()); } app.get('/', routes.index); app.get('/skybiometry/facedetect', skybiometry.facedetect); http.createserver(app).listen(app.get('port'), function(){ console.log('express server listening on port ' + app.get('port')); });
skybiometry.js
exports.facedetect = function(req, res){ res.json('{"used":12,"remaining":88,"limit":100,"reset_time":1395403077,"reset_time_text":"fri, 21 march 2014 11:57:57 +0000"}'); };
when call webservices, have
{"0":"\"","1":"{","2":"\\","3":"\"","4":"u","5":"s","6":"e","7":"d","8":"\\","9":"\"","10":":","11":"1","12":"2","13":",","14":"\\","15":"\"","16":"r","17":"e","18":"m","19":"a","20":"i","21":"n","22":"i","23":"n","24":"g","25":"\\","26":"\"","27":":","28":"8","29":"8","30":",","31":"\\","32":"\"","33":"l","34":"i","35":"m","36":"i","37":"t","38":"\\","39":"\"","40":":","41":"1","42":"0","43":"0","44":",","45":"\\","46":"\"","47":"r","48":"e","49":"s","50":"e","51":"t","52":"_","53":"t","54":"i","55":"m","56":"e","57":"\\","58":"\"","59":":","60":"1","61":"3","62":"9","63":"5","64":"4","65":"0","66":"3","67":"0","68":"7","69":"7","70":",","71":"\\","72":"\"","73":"r","74":"e","75":"s","76":"e","77":"t","78":"_","79":"t","80":"i","81":"m","82":"e","83":"_","84":"t","85":"e","86":"x","87":"t","88":"\\","89":"\"","90":":","91":"\\","92":"\"","93":"f","94":"r","95":"i","96":",","97":" ","98":"2","99":"1","100":" ","101":"m","102":"a","103":"r","104":"c","105":"h","106":" ","107":"2","108":"0","109":"1","110":"4","111":" ","112":"1","113":"1","114":":","115":"5","116":"7","117":":","118":"5","119":"7","120":" ","121":"+","122":"0","123":"0","124":"0","125":"0","126":"\\","127":"\"","128":"}","129":"\""}
instead of :
{"used":12,"remaining":88,"limit":100,"reset_time":1395403077,"reset_time_text":"fri, 21 march 2014 11:57:57 +0000"}
how should modify code ?
is rest api in node.js ?
thanks.
try res.json javascript object. framework automatically create json response:
res.json({ used: 12, remaining: 88, limit: 100, reset_time: 1395403077, reset_time_text: "fri, 21 march 2014 11:57:57 +0000" });
Comments
Post a Comment