这是快递代码
app.post('/v1/sessions' function(req,res){
res.send({id:1234});
});
由于某种原因,json响应像这样返回
OK{ id: 1234}
为什么那里OK?
编辑
好的,这是我所有的代码。我看不出它会在哪里发送 OK。
var express = require('express');
var app = express();
app.enable('trust proxy');
app.use(express.bodyParser());
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.post('/v1/sessions', function(req, res) {
if(req.body.email == 'testuser@captusr.com' && req.body.password == 'testpass'){
res.send(200, JSON.stringify({token:{id:'longstring',email:'testuser@captusr.com'}}));
} else {
res.send({code:403, error:"Invalid email or password"});
}
});
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Origin, Accept');
res.header('Access-Control-Allow-Credentials', 'true');
if (req.method.toLowerCase() === 'options') {
res.send(200);
} else {
next();
}
});
app.listen(3000);
console.log('Listening on port 3000');