Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 05:00:57 09月13日 Update

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!

Tags: Content commands directories problems files versions articles build global tools toolkits projects changes learning help compilation same no middle finger mainstream Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Microsoft Linux Redmi