Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of using nodejs+koa+typescript integration and automatic restart

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to solve the problem of using nodejs+koa+typescript integration and automatic restart". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to solve the problem of using nodejs+koa+typescript integration and automatic restart".

Version description

Node.js: 16.13.1

Create a project

Create the following directory structure

Project ├── src │ └── server.ts ├── package.json └── tsconfig.json

Package.json can be generated using yarn init-y

Tsconfig.json can be built using tsc-- init (you need to install the typescript package globally or in your project to use the tsc command)

Installation dependency

Note:

The @ tsconfig/node16 package needs to be changed according to the version of Node.js. The version of 16.x.x is installed on my computer, so I use @ tsconfig/node16. If you look at the instructions in tsconfig/bases, of course, you don't have to install this package at all. This package has the advantages of commonality and mainstream recommended configuration.

If typescript is already installed globally, remove it from the following command

Concurrently is a toolkit that executes multiple commands concurrently

Nodemon is a toolkit that listens for changes in files and automatically restarts programs.

Yarn add koayarn add typescript @ tsconfig/node16 @ types/node @ types/koa concurrently nodemon-D fill in the content src/server.tsimport Koa from 'koa';const server: Koa = new Koa (); const port: number = 3000 const port server.use ((ctx: Koa.DefaultContext) = > {ctx.body =' hi koa';}); server.listen (port, () = > {console.log (`Node.js v$ {process.versions.node} `);}); tsconfig.json

Note: the value of the extends field is replaced according to the package name @ tsconfig/node** you installed.

{"extends": "@ tsconfig/node16/tsconfig.json", "compilerOptions": {"baseUrl": ".", "rootDir": "src", "outDir": "dist", "noImplicitAny": true,}, "include": ["src/**/*"]} package.json "scripts": {"build-ts": "tsc", "build": "yarn build-ts" "debug": "yarn build & & yarn watch-debug", "serve-debug": "nodemon-- inspect dist/server.js", "serve": "node dist/server.js", "start": "yarn serve", "watch-debug": "concurrently-k-p\" [{name}]\ "- n\" TypeScript,Node\ "- c\" yellow.bold,cyan.bold Green.bold\ "\" npm:watch-ts\ "\" npm:serve-debug\ "," watch-node ":" nodemon dist/server.js "," watch-ts ":" tsc-w "," watch ":" concurrently-k-p\ "[{name}]\"-n\ "TypeScript,Node\"-c\ "yellow.bold,cyan.bold,green.bold\"\ "npm:watch-ts\"\ "npm:watch-node\"} run

All our source code is in the src directory, and the js file compiled by tsc is in the dist directory, which is the path specified in the tsconfig.json file.

Local development: if there is no dist directory, you need to execute yarn build to compile and generate, and then execute yarn watch

Deploy production: execute yarn build, yarn serve, or yarn start sequentially (serve and start are the same command)

The above is all the contents of the article "how to solve the problem of using nodejs+koa+typescript integration and automatic restart". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report