ノード/エクスプレスアプリケーションのバックエンドデータベースとしてMongoDBを使用します。私が直面している問題を要約すると、エクスプレスアプリでbody-parser設定を設定する方法はわかりません。サーバー側のアプリケーションがクライアントアプリケーションによってポストされた完全なJSONを受け取っていないからです(node.jsアプリ)。ほとんどの場合、クライアントは要求本体のJSONをRESTfulエンドポイントに送信しています。ファイルをアップロードする必要があり、それがマルチパート本体なので、私はrequestとform-dataを使用してそのタイプのリクエストを作成し、サーバ側でmulterを使用してマルチパートリクエストを処理しています。そのような要求は処理されません。サーバ側(エクスプレス)でエクスプレスサーバーにJSONデータ(ノード要求付き)をMongoDBにポストする際の問題
、私は、Expressアプリの次の設定があります:私はJSON形式のJavaScriptオブジェクトを構築するには、次のコードを使用しています、ノードクライアントで
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
をnode-requestで、
私は、クライアント側の要求を構成する難しさを持っています::とRESTfulなエンドポイントに投稿私がattem
// class is a JavaScript/JSON object within scope of this code
let req = request.post({
//headers: { 'Content-Type': 'application/json' },
url: 'http://localhost:3000/classes',
//form: class
form: JSON.stringify(class)
}, function (err, resp, body) {
if (err) {
throw err;
}
});
注意をコンテンツタイプをapplication/JSON
として明示的に指定し、JSON.stringify
を使用してJavaScriptオブジェクトをJSON文字列に変換することによって、上記のコードの複数のバージョンを表示できます。
{
"_id" : ObjectId("57758f15f68da08c254ebee1"),
"name" : "Grade 5 - Section A",
"scores" : [{
"studentId" : ObjectId("5776bd36ffc8227405d364d2"),
"performance": [{
"subjectId" : ObjectId("577694ecbf6f3a781759c54a"),
"score" : 86,
"maximum" : 100,
"grade" : "B+"
}]
}]
}
:MongoDBのコレクション(class
)は、二つの他のコレクション(subject
とstudent
)に外部キーを含むドキュメントの以下のタイプを、保存します
Tue, 05 Jul 2016 04:34:46 GMT classReportApp:routes:classes class received from client: { _id: 577b38e65967097c25876764, scores: [] } RangeError: Invalid status code: 0 at ServerResponse.writeHead (_http_server.js:192:11) at ServerResponse.writeHead (C:\Development\classReportApp\node_modules\morgan\node_modules\on-headers\index.js:55:19) at ServerResponse._implicitHeader (_http_server.js:157:8) at ServerResponse.OutgoingMessage.end (_http_outgoing.js:566:10) at ServerResponse.send (C:\Development\classReportApp\node_modules\express\lib\response.js:205:10) at ServerResponse.json (C:\Development\classReportApp\node_modules\express\lib\response.js:250:15) at C:\Development\classReportApp\server-process\app.js:80:26 at Layer.handle_error (C:\Development\classReportApp\node_modules\express\lib\router\layer.js:71:5) at trim_prefix (C:\Development\classReportApp\node_modules\express\lib\router\index.js:310:13) at C:\Development\classReportApp\node_modules\express\lib\router\index.js:280:7 at Function.process_params (C:\Development\classReportApp\node_modules\express\lib\router\index.js:330:12)
スコアサブ文書の配列が空であるのに対して(scores: []
)、クライアント側では、一部の学生のパフォーマンス要素で空でない配列を送信していますその中に。
私はExpressアプリケーションにJSONを投稿する正しい方法に違反しましたか?これをどうやって解決するのですか?
編集:2016年7月5日 私の代わりにextended: true
を使用するためにボディパーサーミドルウェアの設定を変更しました。
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
のNode.jsはまだ次のコードを使用して、POSTリクエストを構成して送信するためにnode-requestモジュールを使用している:
let req = request({
url: 'http://localhost:3000/classes',
method: 'POST',
json: class
}, function (err, resp, body) {
if (err) {
throw err;
}
else {
// process response
}
});
これが機能するようになりましたが、何を私に混乱するコンテンツので、ということですタイプがapplication/jsonの場合、bodyParser.urlencoded({ extended: true })
(またはfalse)はどのように関係しますか?
ノードのバックエンドで受信したJSONは、ドキュメント全体であると想定されていますか? – cs04iz1
@ cs04iz1はい、それは文字通りに見える全体の文書です。 –