This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2623|Reply: 3

Node + Koa2 + TS to create a web application

[Copy link]
Posted on 9/24/2023 12:27:19 PM | | | |
Koa is a new web framework designed by the team behind Express to be a smaller, more expressive, and more powerful foundation for web applications and APIs. By leveraging asynchronous functions, Koa allows you to abandon callbacks and greatly improve error handling capabilities. With no middleware bundled in its core, Koa offers an elegant set of methods to make writing servers quick and enjoyable.

Official Website:The hyperlink login is visible.

GitHub address:The hyperlink login is visible.

Koa2 and Koa1 Difference

The biggest difference between koa2 and koa1 is that koa2 is asynchronous through async/awaite, koa1 is asynchronous through generator/yield, and express is asynchronous through callback functions.



Koa2 requires Node v12.17.0 or later to support ES2015 and asynchronous functions.

First, download and install Node.js, address:The hyperlink login is visible., the installation steps are omitted.

Koa2-based web project

Create a mykoa folder on your computer's disk, then go to the mykoa folder and initialize the project with the cmd command, as follows:

Use npm to install the required packages with the following command:

koa: Core HTTP service
koa-router: Routing module
koa-bodyparser: Responsible for parsing the content of the request

koa-better-body vs. koa-body vs. koa-bodyparser vs



Reference:The hyperlink login is visible.

With minor modifications to the resulting package.json file, the final file reads:

Manually create a new tsconfig.json file with the following contents:

At the same time, create a src/main.ts file with the following contents:

Start the project with npm run start, open it via a browser, as follows:



Send a post request via postman as follows:



Docker runs the Koa project

Create a new Dockerfile to package the project into a docker image, and the file content is as follows:

The command is as follows:




Start a docker container with the following command:



(End)





Previous:[Transfer] SCI, CSSCI, SSCI search database
Next:curl detects information such as the validity period of the https certificate
 Landlord| Posted on 9/24/2023 12:28:33 PM |
Attached is the source code:


mykoa.zip (17.42 KB, Number of downloads: 0, 售价: 10 粒MB)
 Landlord| Posted on 9/24/2023 12:51:24 PM |
When building a docker image, you can reduce the image size by removing unused dependencies after the build is completed:



Originally, node_modules occupies 50M, but after using this command, it only occupies 2.6M
 Landlord| Posted on 9/24/2023 12:57:20 PM |
The latest Dockerfile contents:


Mirror sizeReduced by 40M

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com