Skip to content

API Routes

Creating API Routes

pages/api 中创建一个函数,格式如下

req:

res:

javascript
// req = request data, res = response data
export default (req, res) => {
  // ...
  res.status(200).json({ text: 'Hello' })
}

访问 http://localhost:3000/api

API Routes Details

不能从 getStaticProps or getStaticPaths 获取 API route 。应该直接写在 getStaticProps or getStaticPaths

getStaticProps or getStaticPaths跑在服务端,不会包含浏览器 js bundle,能直接在里面写数据库查询。