Problems with nodejs + koa + typescript integration and automatic restart

Problems with nodejs + koa + typescript integration and automatic restart

Version Notes

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 generated using tsc --init ( typescript package needs to be installed globally or in the project to use the tsc command)

Install Dependencies

Notice:

  • The @tsconfig/node16 package needs to change according to the version of Node.js The version installed on my computer is 16.xx , so I use @tsconfig/node16 . Please refer to the instructions in tsconfig/bases for details. Of course, you don’t need to install this package at all. The advantages of this package are public availability and mainstream recommended configuration.
  • If typescript has been installed globally, remove it from the command below
  • concurrently is a toolkit for executing multiple commands concurrently
  • nodemon is a toolkit that monitors file changes and automatically restarts programs
yarn add koa
yarn add typescript @tsconfig/node16 @types/node @types/koa concurrently nodemon -D

Filling content

src/server.ts

import Koa from 'koa';

const server: Koa = new Koa();
const port: number = 3000;

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 you installed @tsconfig/node**

{
  "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 js files compiled by tsc are in 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

Deployment production: execute yarn build , yarn serve or yarn start in sequence (serve and start are the same command)

References

microsoft/TypeScript-Node-Starter

This is the end of this article about nodejs + koa + typescript integration and automatic restart. For more related nodejs koa typescript content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • nodejs+koa2 implements imitation of springMVC framework
  • Sample code for adding, deleting, modifying and checking json files based on Koa (nodejs framework)
  • How to use koa-log4 to manage nodeJs log notes
  • Typescript nodejs dependency injection implementation code detailed explanation

<<:  Example of implementing hollow triangle arrow and X icon with after pseudo element

>>:  Chinese and English font name comparison table (including Founder and Arphic)

Recommend

How to solve the slow speed of MySQL Like fuzzy query

Question: Although the index has been created, wh...

Causes and solutions for slow MySQL queries

There are many reasons for slow query speed, the ...

MySQL Oracle and SQL Server paging query example analysis

Recently, I have done a simple study on the data ...

Tutorial on installing Android Studio on Ubuntu 19 and below

Based on past experience, taking notes after comp...

Vue realizes the logistics timeline effect

This article example shares the specific code of ...

In-depth explanation of special permissions SUID, SGID and SBIT in Linux

Preface For the permissions of files or directori...

Deep understanding of the mechanism of CSS background-blend-mode

This article is welcome to be shared and aggregat...

Six-step example code for JDBC connection (connecting to MySQL)

Six steps of JDBC: 1. Register the driver 2. Get ...

Detailed steps for deploying Microsoft Sql Server with Docker

Table of contents 1 Background 2 Create a contain...

Bootstrap realizes the effect of carousel

This article shares the specific code of Bootstra...

Analysis and solution of MySQL connection throwing Authentication Failed error

[Problem description] On the application side, th...

Detailed explanation of JavaScript timers

Table of contents Brief Introduction setInterval ...

Detailed example of using if statement in mysql stored procedure

This article uses an example to illustrate the us...

Three Ways to Find the Longest Word in a String in JavaScript (Recommended)

This article is based on the Free Code Camp Basic...