Expressjs のルート パラメータ
3991 ワード
序章
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys. Expressjs official Docs
サンプルコードのアプリで route( ) を定義したとしましょう:
const express = require('express')
const app = express()
// a route that takes params:
app.get('/users/:userId/books/:bookId', (req, res) => {
// we can extract parameters from the route from req.params object
const userId = req.params.userId
const bookId = req.params.bookId
// use userId and bookId values to do something useful
})
次のようなものにマップします.
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
重要な注意点:
クライアントのリクエストからの入力をサニタイズして検証することが重要です.リクエストはユーザーが構築したデータであり、何でも含めることができます.考えられるあらゆる種類のデータに対してサニタイズを実行するために使用できるライブラリがあります.
概要
Route パラメータは、リクエスト URL 内でアプリにデータを渡す必要がある場合に便利です.アプリからこれらの値を抽出し、Redis ストアなどからアイテムまたはその他のデータを検索して、HTTP 応答内で意味のあるデータを返す場合があります.
リクエストから受信したデータは、常にサニタイズして検証することを忘れないでください.リクエストはユーザーが作成し、何でも含めることができます.
次に、次の内容について説明します.
次に、次の内容について説明します.
すべてのサンプル コードは github のホストです
立ち寄ってくれてありがとう!明けましておめでとうございます.エネルギーがあなたと共にありますように!
Reference
この問題について(Expressjs のルート パラメータ), 我々は、より多くの情報をここで見つけました https://dev.to/naftalimurgor/route-params-in-expressjs-m1cテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol