我正在尝试使用nodejs和expressjs实现web服务。
这是我的代码:
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 only
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"}');
};
当我打电话给网络服务时,我有这个
{"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":"\""}
而不是:
{"used":12,"remaining":88,"limit":100,"reset_time":1395403077,"reset_time_text":"Fri, 21 March 2014 11:57:57 +0000"}
我应该如何修改我的代码?
这是node.js中的rest api吗?
感谢。
答案 0 :(得分:0)
使用JavaScript对象尝试res.json。该框架将自动创建一个JSON响应:
res.json({
used: 12,
remaining: 88,
limit: 100,
reset_time: 1395403077,
reset_time_text: "Fri, 21 March 2014 11:57:57 +0000"
});
答案 1 :(得分:0)
您的字符串基本上只是一个JSON格式的文本字符串。它还不是JSON对象。
要实现此目的,您需要将字符串解析为JSON,而使用JSON.parse()
要使用您的代码执行此操作,您可以像这样编写
exports.facedetect = function(req, res){
var jsonCode = {
"used":12,
"remaining":88,
"limit":100,
"reset_time":1395403077,
"reset_time_text":"Fri, 21 March 2014 11:57:57 +0000"
}
res.json(JSON.parse(jsonCode));
};