Home>Article>Web Front-end> Teach you step by step how to deploy a TS Node.js project correctly and quickly!

Teach you step by step how to deploy a TS Node.js project correctly and quickly!

青灯夜游
青灯夜游 forward
2021-10-14 10:18:12 5761browse

How to deploy a TSNode.jsproject correctly and quickly? The following article will teach you how to deploy a TS Node.js application in a few minutes. I hope it will be helpful to you!

Teach you step by step how to deploy a TS Node.js project correctly and quickly!

As a full-stack developer, it is very interesting to create projects. You can design the architecture, brainstorm, and develop, but after the development is completed, we have to deploy or release application. So how to deploy a TS Node.js project correctly and quickly? Let’s get it done today. [Recommended learning: "nodejs Tutorial"]

Create a TS Node.js application

If you are already familiar with creating a TS Node.js project , you can jump directly to the "Deploy and Release Application" section

Initialize the Node.js project:

In our team, we really like TS and use it in all our new projects TS is used in every project, so creating a TS project is nothing new.

Let’s start with the basics:

  • npm initInitialize a Node.js project using-yParameters can quickly skip step-by-step configuration

  • ##npm install express @types/expressInstall express dependencies, and express types file for TS development

  • npm install typescript --save-devInstall typescript as a development dependency

  • mkdir my-app && cd my-app npm init -y npm install express @types/express --save npm install typescript --save-dev

TS configuration

  • npx tsc --initwill create a typescript default configuration file tsconfig.json
  • declarationused to specify whether to compile After completion, the corresponding *.d.ts file is generated. The default is false
  • outdirDefine the directory after TS compilation. If there is no declaration, the default compiled file location will be the same as the ts source file. In the same location
Run the command

npx tsc --init

Modify the following configuration

"compilerOptions": { ... "outDir": "dist", // 编译后输出目录 "declaration": true // 生成 d.ts }

Create the project entry file

Create

server.tsFile

import express from 'express' const app = express() const PORT = 3000 app.use(express.json()) app.get('/', (req, res) => { res.send(‘Hello World!’) }) app.listen(PORT, () => { console.log(`Server is listening on port ${PORT}`) })

After completing the above steps, our file directory structure is as follows

. ├── node_modules ├── package-lock.json ├── package.json ├── server.ts └── tsconfig.json

Compile TS

Our next step is to build and deploy our TS Node.js application. Since in the production environment, we do not run the TS version, but the compiled JS. Now let’s compile the project

Modify the package.json file and add the following command

  • npm run tscwill be compiled according to the configuration of our tsconfig.json Our project and output to the specified directory

  • ##npm run start:prod

    will run our compiled JS file

    "scripts": { "tsc": "tsc", "start:prod": "node dist/server.js" }
  • Then test locally
npm run tsc npm run start:prod # 服务启动成功,运行端口:3000

Access http://localhost:3000/ through the browser, the access is successful, then we deploy and publish our application

Teach you step by step how to deploy a TS Node.js project correctly and quickly!

Deploy and publish applications

Here we mainly use two methods to distribute and deploy the compiled TS project to various environments

The form of npm dependency package
  • docker container method
The form of NPM dependency package

NPM life cycle hook

Some special life cycle hooks will be triggered when the specified operation is triggered. Here we will use the "prepare" hook, which will be triggered once before executing the npm publish command to publish to NPM. So we can compile the TS application at this time.

Specify publishing files

Through the "files" field we can define which files should be included when publishing the NPM package. If this attribute is omitted, the default will be ["*" ], all files will be uploaded.

The following is the modified package.json

"name": "my-app-xiaoshuai", // 我们发布到NPM上的名字 "main": "dist/server.js", // 修改入口文件地址 "types": "dist/server.d.ts", // 指定TS类型文件 "files": [ "dist", "package.json", "package-lock.json", "README.md" ], "scripts": { "tsc": "tsc", "prepare": "npm run tsc" // 编辑typescript }

npm publish

After modifying the package.json configuration, we run the npm publish command, Publish our application to NPM

npm publish

Output

Teach you step by step how to deploy a TS Node.js project correctly and quickly!After successful publishing, you can see that there is an additional

my-app- on npmjs xiaoshuai

Package

Teach you step by step how to deploy a TS Node.js project correctly and quickly!

Docker container method

To publish our TS Node.js application as a container, we need Create a docker configuration file Dockerfile in the project root directory.

Let’s write the Dockerfile step by step

    Copy the compiled file into the container
  • Copy package.json and package-lock.json into the container
  • Use
  • npm install

    Install dependencies

  • Use
  • node build/ server.js

    Run our application

# Node 版本 FROM node:14.18.0-alpine ARG NODE_ENV=production ENV NODE_ENV $NODE_ENV COPY ./dist /dist COPY ./package.json /package.json COPY ./package-lock.json /package-lock.json RUN NODE_ENV=$NODE_ENV npm install EXPOSE 3000 CMD ["node", "dist/server.js"]

现在我们可以在根目录中构建docker镜像,运行docker build --tag my-app:test .命令

docker build --tag my-app:test .

成功后输出如下

Teach you step by step how to deploy a TS Node.js project correctly and quickly!

接着我们运行容器,使用docker run -p 3000:3000 -it my-app:test命令来运行我们的应用,可以看到程序成功运行在3000端口

docker run -p 3000:3000 -it my-app:test # 服务启动成功,运行端口:3000

通过浏览器访问http://localhost:3000/,访问成功

Teach you step by step how to deploy a TS Node.js project correctly and quickly!

源码

https://github.com/cmdfas/ts-node-express-deploy

总结

今天我们介绍了创建TS Node.js项目和部署它的基础知识,希望对大家有所帮助,能够用在现在或未来的项目中。

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Teach you step by step how to deploy a TS Node.js project correctly and quickly!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete