typescript全栈_使用TypeScript快速创建全栈Web应用

typescript全栈

使用TypeScript快速创建全栈Web应用 (Quickly Create a Full-Stack Web App in TypeScript)

使用Vue.js 3,Node.js和PostgreSQL (Using Vue.js 3, Node.js, and PostgreSQL)

Photo by the author.
图片由作者提供。

In this article, we are going to build a simple task management application in TypeScript with Vue.js 3, Node.js, and PostgreSQL using Kretes. Here’s what the final result looks like:

在本文中,我们将使用Kretes在TypeScript中使用Vue.js 3,Node.js和PostgreSQL构建一个简单的任务管理应用程序。 最终结果如下所示:

A simple task management application written in Kretes.
用Kretes编写的简单任务管理应用程序。

Kretes is a batteries-included programming environment for building modern, fully featured web applications using TypeScript. It combines a framework (similar to Rails or Laravel) with an integrated editor based on Visual Studio Code.

Kretes是一个包含电池的编程环境,用于使用TypeScript构建现代的功能齐全的Web应用程序。 它结合了一个框架(类似于Rails或Laravel)和一个基于Visual Studio Code的集成编辑器。

Kretes uses only one programming language (TypeScript) for the entire application. In other words, you write TypeScript on the front end and the back end. By using TypeScript across the board, there is just one ecosystem and one tool to manage and there are fewer concepts to learn. Thus, everything is simpler and more unified.

Kretes对于整个应用程序仅使用一种编程语言(TypeScript)。 换句话说,您在前端和后端上编写TypeScript。 通过全面使用TypeScript,只有一种生态系统和一种工具可以管理,学习的概念也更少。 因此,一切都变得更加简单和统一。

The most important idea behind Kretes is that you don’t need to worry about dependencies or configuration. Most of that is taken care of so that you can focus on solving the actual needs of your application.

Kretes背后最重要的想法是,您不必担心依赖关系或配置。 其中大部分都得到了照顾,因此您可以专注于解决应用程序的实际需求。

Instead of giving you a definition, let me show you how to build a web application in TypeScript with front-end, back-end, and database integration — all in about 10 minutes.

除了给您一个定义外,让我向您展示如何在TypeScript中通过前端,后端和数据库集成来构建Web应用程序-大约需要10分钟。

先决条件 (Prerequisites)

Follow the official documentation to install Kretes on your machine. You need three elements: the Nix package manager, the Kretes extension for VS Code, and pgcli (a PostgreSQL command-line utility).

按照官方文档在您的计算机上安装Kretes。 您需要三个元素: Nix程序包管理器 ,VS Code的Kretes扩展pgcli (PostgreSQL命令行实用程序)。

Optionally, you can install Kretes using NPM if you’d like to use it via the command line or with another editor.

另外,如果您想通过命令行或其他编辑器使用KPM,可以使用NPM安装Kretes。

初始化应用程序 (Initialize the Application)

Almost everything in Kretes is done directly in VS Code. Once the Kretes extension is installed, you can initialize a new project by using Kretes: New Project from the command palette. Choose a name for your application (e.g. kretes123) and the folder where it should be placed:

Kretes中的几乎所有内容都直接在VS Code中完成。 一旦安装了Kretes扩展,您就可以使用Kretes: New Project从命令面板中的Kretes: New Project来初始化一个新项目。 为您的应用程序选择一个名称(例如kretes123 )和应放置该文件的文件夹:

This will create your application along with a few directories and files that comprise the initial structure. Look around! The features/ and config/ are the most important directories right now.

这将创建您的应用程序以及一些包含初始结构的目录和文件。 看看周围! 现在, features/config/是最重要的目录。

安装依赖项 (Install Dependencies)

To install the dependencies for your application, use the Kretes: Install task. By default, Kretes uses the pnpm package manager, as it’s much faster and disk-space-efficient compared to npm.

要为您的应用程序安装依赖项,请使用Kretes: Install任务。 默认情况下,Kretes使用PNPM包管理器,因为它的速度更快,并且与磁盘空间效率的npm

开始申请 (Start Your Application)

To start your application, use the Kretes: Start task. You can trigger it by using the Tasks: Run Task command from the Command Palette in VS Code:

要启动您的应用程序,请使用Kretes: Start任务。 您可以使用VS Code中的“ 命令面板”中的“ Tasks: Run Task命令来触发它:

Let’s navigate to localhost:5544, where our application welcomes us with the Hello, Kretes message:

让我们导航到localhost:5544 ,在这里我们的应用程序用Hello, Kretes消息欢迎我们:

构建全栈单页应用程序(SPA) (Building a Full-Stack Single-Page Application (SPA))

OK, but this is just a simple HTML page. Let’s do something a little bit fancier. We will build a task management app with front-end, back-end, and database integration — all in about 10 minutes or so. Do you think that’s possible?

好的,但这只是一个简单HTML页面。 让我们做一些更奇特的事情。 我们将在大约10分钟左右的时间内构建一个具有前端,后端和数据库集成的任务管理应用程序。 您认为有可能吗?

We will use Vue.js version 3 along with its Composition API and Tailwind CSS for simplicity on the front end. Open VS Code’s terminal and install Vue.js 3 using pnpm:

为了简化前端,我们将使用Vue.js版本3以及其Composition API和Tailwind CSS。 打开VS代码的终端,并使用安装Vue.js 3 PNPM

pnpm add vue@3.0.0-beta.22

Note: You need to specify the Vue.js version explicitly, as it’s currently still in beta.

注意:您需要明确指定Vue.js版本,因为它目前仍处于测试阶段。

In config/client/index.html, we need to create a Vue.js instance using the createApp function and then attach it (or mount it, per Vue.js’s wording) to the div element with the app identifier:

config/client/index.html ,我们需要使用createApp函数创建一个Vue.js实例,然后将其附加(或按照Vue.js的措辞将其安装)到带有app标识符的div元素上:

The App component doesn’t exist yet. Let’s go ahead and create it in features/Base/View as App.vue. We need the <template> part and the <script> part:

App组件尚不存在。 让我们继续在features/Base/View App.vue其创建为App.vue 。 我们需要<template>部分和<script>部分:

Since this is a full-stack app, asynchronous communication is needed between the front end and back end. In the template part, we will use the suspense feature. There will be two scenarios:

由于这是一个全栈应用程序,因此前端和后端之间需要异步通信。 在模板部分,我们将使用suspense功能。 将有两种情况:

  1. The default one where we display a list of tasks.

    默认的是我们显示任务列表的地方。
  2. The fallback that shows the loading message.

    显示加载消息的后备。

Our script section is simple. We only reference another component to keep the responsibilities nicely separated. A task is a feature of our application, so let’s create Task.vue inside the features/Task/View directory:

我们的脚本部分很简单。 我们仅引用另一个组件来使职责完全分开。 任务是我们应用程序的功能,因此让我们在features/Task/View目录中创建Task.vue

We will have a text field for entering new tasks and a list of tasks in a container so it’s nicely placed. Let’s add the input text field along with the button:

我们将有一个用于输入新任务的文本字段,以及一个容器中的任务列表,以便放置在容器中。 让我们添加输入文本字段以及按钮:

Next, let’s take care of the list of tasks. We will have a task-item with a label and a custom checkbox along with the name of a particular task:

接下来,让我们处理任务列表。 我们将有一个task-itemlabel ,并用特定的任务的名称以及相应的自定义复选框:

Let’s save. The changes are reflected right away! That’s pretty cool, huh?

让我们保存。 更改立即反映出来! 太酷了吧?

Did you notice that we didn’t install any bundler like Webpack or rollup? It works out of the box with zero configuration. You no longer need to waste countless hours figuring out why the configuration isn’t working. Kretes has got your back!

您是否注意到我们没有安装任何打包工具,例如Webpack或汇总工具? 它采用零配置开箱即用。 您不再需要浪费大量的时间来弄清楚为什么配置不起作用。 克雷特斯得到了你的支持!

RESTful API (RESTful API)

That’s still only the front-end part. Let’s persist our tasks in a database and fetch it over the wire using a RESTful API. This way, we will see how to connect the front end to the back end and also how to interact with this database.

那仍然只是前端部分。 让我们将我们的任务保留在数据库中,并使用RESTful API通过电线将其提取。 这样,我们将看到如何将前端连接到后端,以及如何与该数据库交互。

First, we need a REST API with an endpoint that returns a collection of tasks. Luckily, RESTful endpoints are created automatically in Kretes as long as you follow a simple convention: You need to place a specific handler function in a Controller of a given feature.

首先,我们需要一个REST API,该API的端点将返回任务的集合。 幸运的是,只要遵循一个简单的约定,就可以在Kretes中自动创建RESTful端点: 您需要在给定功能的Controller中放置特定的处理函数。

Let’s do that. Our feature is Task and we create the Controller directory inside features/Task/ and inside a file named browse.ts that will hold a handler being triggered using the GET HTTP method:

来做吧。 我们的功能是Task ,我们在features/Task/并在名为browse.ts的文件内创建Controller目录,该文件将保存使用GET HTTP方法触发的处理程序:

For now, let’s put our task collection in memory as a variable:

现在,让我们将任务集合作为变量放入内存中:

Since this is the Task feature, this translates to the /task path. Let’s trigger this endpoint with an HTTPGET request. We will use httpie, a command-line HTTP utility:

由于这是Task功能,因此它转换为/task路径。 让我们通过HTTP GET请求触发此端点。 我们将使用httpie (命令行HTTP实用程序):

http :5544/task

Note: You can also just open the localhost:5544/task in your browser to see the result.

注意:您也可以 在浏览器中 打开 localhost:5544/task 来查看结果。

It works. Have you noticed we didn’t restart our app? We can just focus on the business logic of our application.

有用。 您是否注意到我们没有重新启动我们的应用程序? 我们可以只关注应用程序的业务逻辑。

数据库整合 (Database Integration)

Let’s move these tasks from in memory to a database. We haven’t really talked about databases yet, but that’s not a problem. In config/default.yml, you will see Kretes expects a database named after the project. In our case, the database is named kretes123.

让我们将这些任务从内存中移到数据库中。 我们还没有真正谈论数据库,但这不是问题。 在config/default.yml ,您将看到Kretes需要一个以项目命名的数据库。 在我们的例子中,数据库名为kretes123

We haven’t created this database. We also haven’t yet installed any database engine, such as PostgreSQL or MySQL. But let’s go ahead and connect to this database anyway. We will use pgcli, a command-line PostgreSQL utility. Open the terminal in VS Code and type the following command:

我们尚未创建此数据库。 我们还没有安装任何数据库引擎,例如PostgreSQL或MySQL。 但是,让我们继续并连接到该数据库。 我们将使用pgcli (一个命令行PostgreSQL实用程序)。 在VS Code中打开终端,然后键入以下命令:

pgcli kretes123

It works — we connected to a database. Behind the scenes, Kretes installed PostgreSQL, started the database engine, and created a database for you with the right name and permissions. This way, you can keep focusing on creating the application instead of wasting any time on configuration. That’s cool, isn’t it?

它有效-我们已连接到数据库。 在后台,Kretes安装了PostgreSQL,启动了数据库引擎,并使用正确的名称和权限为您创建了一个数据库。 这样,您可以继续专注于创建应用程序,而不用浪费任何时间进行配置。 很好,不是吗?

Let’s quickly create a table and insert our data. Still in the terminal, inside the pgcli connection, type the following SQL statements:

让我们快速创建一个表并插入我们的数据。 仍然在终端的pgcli连接中,键入以下SQL语句:

Once you finish, you can exit pgcli with \q and close the terminal window.

完成后,可以使用\q退出pgcli并关闭终端窗口。

Use pgcli to create a table in the database and insert some data.
使用pgcli在数据库中创建一个表并插入一些数据。

Finally, let’s query the database and return the task collection directly from our storage solution via the RESTful endpoint we created earlier. We need to import the database namespace. For convenience, we’re aliasing it to db. Then, in our handler function, we ask for all the rows from the task table we’ve just created a minute ago:

最后,让我们查询数据库并通过我们先前创建的RESTful端点直接从存储解决方案返回任务集合。 我们需要导入database名称空间。 为了方便起见,我们将其别名为db 。 然后,在处理程序函数中,我们从刚才创建的task表中请求所有行:

Let’s test it on the command line, again using httpie (or you can open localhost:5544/task in your browser):

让我们再次使用httpie在命令行上对其进行测试 (或者您可以在浏览器中打开localhost:5544/task ):

http :5544/task
/task to test getting data from the database. /task上使用httpie测试从数据库中获取数据。

It works. Again, we didn’t restart the application and the changes are reflected immediately. That’s a nice programming flow, isn’t it?

有用。 同样,我们没有重新启动应用程序,更改立即反映出来。 那是一个很好的编程流程,不是吗?

We can now connect this RESTful endpoint to our Vue.js component. Since we are using TypeScript everywhere, we can define the shape of our data and the application state using TypeScript’s interfaces:

现在,我们可以将此RESTful端点连接到我们的Vue.js组件。 由于我们在各处都使用TypeScript,因此可以使用TypeScript的接口定义数据的形状和应用程序状态:

We need to initialize the component state:

我们需要初始化组件状态:

We can now fetch the task collection directly from our Vue.js component as we did on the command line. I will use the ky library for that. Let’s first install it using pnpm:

现在,我们可以像在命令行上一样直接从Vue.js组件中获取任务集合。 我将使用ky库。 首先使用pnpm安装它:

pnpm add ky

And let’s trigger a GET request for the /task path directly from our Vue.js component:

让我们直接从我们的Vue.js组件触发对/task路径的GET请求:

Finally, let’s iterate over the task collection in the Vue.js component:

最后,让我们遍历Vue.js组件中的任务集合:

Once you save, it should be immediately visible in your browser. Try adding a task to the list:

保存后,它应该立即在浏览器中可见。 尝试将任务添加到列表中:

The final application.
最终的应用程序。

That’s all. Fewer restarts and all dependencies are managed for you. You can focus on the joy of programming.

就这样。 重新启动的次数更少,并且为您管理所有依赖项。 您可以专注于编程的乐趣。

I hope you find Kretes useful.

我希望您发现Kretes有用。

Feel free to ask any questions in the comments section below.

随时在下面的评论部分中提问。

If you are more of a visual learner, check out the video below for a step-by-step tutorial:

如果您是一名视觉学习者,请查看下面的视频以获取分步教程:

A step-by-step tutorial on how to build a full-stack TypeScript app using Kretes with Vue.js, Node.js, and PostgreSQL.
有关如何使用Kretes和Vue.js,Node.js和PostgreSQL构建全栈TypeScript应用程序的分步教程。

奖励:连接添加按钮 (Bonus: Connecting the Add Button)

Would you like to learn how to connect the Add button in our task management application so that each new task is also persisted in the database? Let me know in the comments section and I’ll write a follow-up to this article.

您是否想学习如何连接任务管理应用程序中的“ Add按钮,以便每个新任务也都保留在数据库中? 在评论部分让我知道,我将写这篇文章的后续文章。

资源资源 (Resources)

  • HTTPie — a user-friendly command-line HTTP client for the API era.

    HTTPie -API时代的用户友好命令行HTTP客户端。

  • ky — Tiny & elegant HTTP client based on window.fetch

    ky —基于window.fetch的小巧优雅的HTTP客户端

  • Kretes — Programming Environment for TypeScript & Node.js

    Kretes — TypeScript和Node.js的编程环境

  • pnpm — Fast, disk space efficient package manager

    pnpm —快速,节省磁盘空间的软件包管理器

  • pgcli — a command-line utility for PostgreSQL

    pgcli — PostgreSQL命令行实用程序

翻译自: https://medium.com/better-programming/quickly-create-a-full-stack-web-app-in-typescript-9188f011748b

typescript全栈

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值