zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[Next.js] Hide Sensitive Information from the Consumers of Next.js API

JSAPI The of from Next information Hide
2023-09-14 08:59:12 时间

We'll learn how to use Next.js API Routes to hide sensitive information from the clients. In this case, we're calling the JSON Placeholder API with a "secret" value in the headers. All that sensitive information is hidden from the clients since they don't call, or even know, that we're calling the JSON Placeholder API under the hood.

 

async function getSuperSecretData() {
  const result = await fetch("https://jsonplaceholder.typicode.com/todos/1", {
    headers: {
      authorization: 'SUPER SECRET VALUE'
    }
  }).then(res => res.json())

  return result
}

async function handler(req, res) {
  const secretTodo = await getSuperSecretData()

  res.json({todo: secretTodo})
}

export default handler

authorization header won't be seen from the request in backend