2012-02-17 29 views

Trả lời

44

Tôi đã tìm thấy câu trả lời. Một trong các plugin được bao gồm cần được kích hoạt, . Sau đó, dữ liệu có thể được tìm thấy trong req.params (mặc định) hoặc req.body (mapParams: false), tùy thuộc vào số settings (xem cụ thể tại phần BodyParser).

Ví dụ:

server.use(restify.bodyParser({ mapParams: false })); // mapped in req.body 

Hoặc:

server.use(restify.bodyParser()); // mapped in req.params 
5

là rất đơn giản:

server.use(restify.bodyParser({ mapParams: false })); 

Bạn cần kích hoạt bodyParser trong restify

1

Mã này sẽ in cơ thể yêu cầu ra cửa sổ Console:

var restify = require('restify'); 
var server = restify.createServer(); 

// This line MUST appear before any route declaration such as the one below 
server.use(restify.bodyParser()); 

server.post('/customer/:id', function (req, resp, next) { 
    console.log("The request body is " + req.body); 
    response.send("post received for customer " + req.params.id + ". Thanks!"); 
    return next(); 
}); 
3

Đối restify 5.0.0+, sử dụng:

server.use(restify.plugins.bodyParser()); 

https://github.com/restify/node-restify/issues/1394#issuecomment-312728341

Đối với các phiên bản cũ sử dụng:

server.use(restify.bodyParser()); 

Sau khi nói restify sử dụng bodyParser middleware theo yêu cầu cơ thể sẽ có sẵn theo yêu cầu đối tượng thuộc tính nội dung:

server.post('/article', (req, res, next) => { 
    console.log(req.body) 
    next() 
}) 
Các vấn đề liên quan