【问题标题】:Create REST API without using any backend在不使用任何后端的情况下创建 REST API
【发布时间】:2018-10-28 07:43:03
【问题描述】:

如何在不使用node、express等后端技术的情况下创建一个rest api?即:是否可以仅使用客户端框架(如 react、vue)创建 api 而不涉及服务器端?

我想创建一个非常简单的 rest api,我可以将它托管在 github 页面或 gitlab 页面上。

我想要一个可以通过我自己的域(例如 http://username.github.io)访问的 live rest api,而不是你可以使用 json-serverMy JSON Server 创建的假的 - 并且能够执行以下操作。

这里是api应该做什么的描述。

/quotes.txt - 读取quotes.txt文件的内容并显示出来

/quotes.json - 读取quotes.txt文件的内容并进行转换 转json格式

/random.txt - 读取quotes.txt文件的内容并显示一个 txt格式的随机引用

/random.json - 读取quotes.txt文件的内容并显示一个 json格式的随机引用

API 的输出

/quotes.txt

To be or not to be that is the question - Author Unknown
All your dreams can come true if you have the courage to pursue them - Walt Disney
Stand up for what you believe in even if you are standing alone - 

/quotes.json

[
  {
    "quote": "To be or not to be that is the question",
    "author": "Author Unknown"
  },
  {
    "quote": "All your dreams can come true if you have the courage to pursue them",
    "author": "Walt Disney"
  },
  {
    "quote": "Stand up for what you believe in even if you are standing alone",
    "author": ""
  }
]

/random.txt

All your dreams can come true if you have the courage to pursue them - Walt Disney

/random.json

  {
    "quote": "All your dreams can come true if you have the courage to pursue them",
    "author": "Walt Disney"
  }

【问题讨论】:

  • 没有后端,您能详细说明一下吗?

标签: json rest api backend static-site


【解决方案1】:

您可以使用 json-server 来提供 JSON 文件。它支持表达 RESTful 请求的“规范”方式。这个工具可以在开发机器上使用,也可以在 Intranet 服务器上使用。这是一个blog post,关于如何设置它。

My JSON Server 是一项在 Internet 环境中为您执行相同操作的服务。还有具有类似功能的竞争服务,例如myjson.com。如果您在线搜索,我相信您会找到更多。


附注我相信你需要做一些按摩才能真正翻译.txt -> .json。我从未见过针对.txt 文件而不是.json 文件的服务。

呃。我真的很喜欢使答案无效的问题编辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多