使用Webtask.io创建一个简单的REST API端点

by ismapro

由ismapro

使用Webtask.io创建一个简单的REST API端点 (Create a simple REST API endpoint using Webtask.io)

Webtask.io is a service by Auth0 that allows you to run single pieces of code in the cloud through HTTP calls.

Webtask.io是Auth0提供的一项服务,它使您可以通过HTTP调用在云中运行单个代码。

Each deployed piece will run under a sandbox with some limitations:

每个部署的组件都将在沙盒下运行,但有一些限制:

  • limited processor time

    处理器时间有限
  • a limited amount of libraries available per task

    每个任务可用的库数量有限
  • limited storage

    有限的存储

But these limitations serve to present an environment where you can expose your application through HTTP, in easy and scalable way, without getting into the nitty-gritty of server administration or environment configurations.

但是这些限制有助于提供一个环境,您可以在其中通过HTTP以简单且可扩展的方式公开应用程序,而不必费心服务器管理或环境配置。

There are many other features available like token validation to control access, secret data, and metadata. If you want to know more of how Webtask works, their documentation has examples of what you can do with this technology.

还有许多其他功能可用,例如令牌验证以控制访问,秘密数据和元数据。 如果您想更多地了解Webtask的工作方式,那么他们的文档中提供了使用该技术的示例。

让我们开始编写基本的REST API (Let’s start to code the basic REST API)

To create a webtask, you’ll need to use the webtask-cli. This is a command line application that allows you to manage your webtasks.

要创建Web任务,您需要使用webtask-cli。 这是一个命令行应用程序,可让您管理Web任务。

So first install it in your enviroment:

因此,首先将其安装在您的环境中:

npm install wt-cli -g

Then initialize your session, using this email login process:

然后使用此电子邮件初始化您的会话 登录过程:

wt init your_email@something.com

Once you do so, you should receive a code to activate your account.

完成后,您应该会收到一条代码来激活您的帐户。

Now you can proceed to create the file that will be logic of our webtask. You can name it whatever you want, but remember that it will be part of the URL the service will later provide. Let’s name it:

现在,您可以继续创建将成为我们Web任务逻辑的文件。 您可以随意命名,但请记住,它将成为服务稍后提供的URL的一部分。 命名为:

basic-rest.js

and lets add the following code to it:

并向其中添加以下代码:

Navigate from the command line to the location where you saved the file and run this command:

从命令行导航到保存文件的位置,然后运行以下命令:

wt create basic-rest.js

You will receive a URL that you can use to check your webtask, similar to this:

您将收到一个可用于检查Web任务的URL,类似于以下内容:

https://webtask.it.auth0.com/api/run/wt-myemail-gmail_com-0/basic-rest?webtask_no_cache=1

From your browser navigate to the your url and you will see the response of your application:

从浏览器导航到您的网址,您将看到应用程序的响应:

{"error":"GET method not implemented"}

Which is the response we expected from our code. Now you can add any logic you want to each one of the methods. You can then test the other methods (POST, DELETE, PUT) using postman or curl.

这是我们期望从代码中得到的响应。 现在,您可以向每种方法添加所需的任何逻辑。 然后,您可以使用邮递员或curl测试其他方法(POST,DELETE,PUT)。

And that’s it. You have deployed a service without any additional configuration or administration. The great thing about this service is the ability to integrate webhooks from external APIs and interact with the data or query using other backends.

就是这样。 您已经部署了服务,而没有任何其他配置或管理。 这项服务的最大优点是能够集成来自外部API的webhook并使用其他后端与数据或查询进行交互。

There are many features and options I didn’t explored but you can check their webpage and now more about them.

我没有探索过许多功能和选项,但您可以查看它们的网页 ,现在更多有关它们的信息。

Hope you liked, let me know what you think in the comments sections. Happy coding!

希望您喜欢,让我知道您在评论部分的想法。 祝您编码愉快!

翻译自: https://www.freecodecamp.org/news/create-a-simple-rest-api-endpoint-using-webtask-io-d9607fc00c17/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值