Home > Article > Web Front-end > Teach you step by step how to deploy a TS Node.js project correctly and quickly!
How to deploy a TS Node.js project 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!
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"]
If you are already familiar with creating a TS Node.js project , you can jump directly to the "Deploy and Release Application" section
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 init
Initialize a Node.js project using -y
Parameters can quickly skip step-by-step configuration
##npm install express @types/express Install express dependencies, and express types file for TS development
npm install typescript --save-dev Install 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
will create a typescript default configuration file tsconfig.json
used to specify whether to compile After completion, the corresponding *.d.ts file is generated. The default is false
Define 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
npx tsc --initModify the following configuration
"compilerOptions": { ... "outDir": "dist", // 编译后输出目录 "declaration": true // 生成 d.ts }
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
npm run tsc will be compiled according to the configuration of our tsconfig.json Our project and output to the specified directory
will run our compiled JS file
"scripts": { "tsc": "tsc", "start:prod": "node dist/server.js" }
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
Deploy and publish applicationsThe 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
npm publish
Output
After successful publishing, you can see that there is an additional
my-app- on npmjs xiaoshuaiPackage
Let’s write the Dockerfile step by step
Install dependencies
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 .
成功后输出如下
接着我们运行容器,使用docker run -p 3000:3000 -it my-app:test
命令来运行我们的应用,可以看到程序成功运行在3000端口
docker run -p 3000:3000 -it my-app:test # 服务启动成功,运行端口:3000
通过浏览器访问http://localhost:3000/,访问成功
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!