Mean Stack的全新功能,但通过遵循各种教程,已经设法在dockerized环境中进行了设置。我主要使用https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf
中解释的内容我遇到的问题是要获取对自动编译的TypeScript文件的更改并重新启动serice失败,并且我不确定配置中缺少什么。我尝试使用nodemon和ts-node-dev进行各种迭代,但均未成功。
服务器启动并提供100%的页面,但是代码更改未触发任何操作。
以下是Dockerfile(用于Node Express服务器的 ),package.json和我的tsconfig.json,如果有人可以指出我要去哪里的话,那将是一个很大的帮助。我还显示了整个堆栈的docker-compose.yml文件。
Dockerfile
FROM node:latest
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
RUN npm install --save body-parser express mongoose
RUN npm install --save nocache
RUN npm install --save nodemon typescript ts-node ts-node-dev
RUN npm install --save-dev tsc-watch
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 3000 27017
# Also tried starting with 'dev'
CMD [ "npm", "run", "prod" ]
package.json
{
"name": "apis-project",
"version": "1.0.0",
"description": "https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf",
"main": "index.js",
"scripts": {
"build": "tsc",
"dev": "ts-node ./lib/server.ts",
"start": "nodemon ./dist/server.js",
"prod": "npm run build && npm run start"
},
"keywords": [
"nodejs",
"typescript"
],
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.1",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.7.7",
"nodemon": "^1.19.4"
},
"devDependencies": {
"ts-node-dev": "^1.0.0-pre.43"
}
}
我在脚本部分尝试过的其他变体是:
"dev2": "ts-node-dev --respawn --transpileOnly ./lib/server.ts",
"dev3": "nodemon --watch 'lib/*.ts' --exec 'ts-node' lib/server.ts",
"dev5": "tsc-watch ./lib/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",
"dev6": "tsc-watch ./lib/*.ts --outDir ./dist --onSuccess \"node ./dist/server.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",
"dev7": "tsc-watch ./lib/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",
"x-compile": "tsc && node ./dist/server.js",
"x-dev": "./node_modules/nodemon/bin/nodemon.js -e ts --exec \"npm run x-compile\"",
tsconfig.json
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"target": "es6",
"outDir": "./dist",
"baseUrl": "./lib"
},
"include": [
"lib/**/*.ts"
],
"exclude": [
"node_modules"
]
}
docker-compose.yml
version: '3' # specify docker-compose version
# Define the services/containers to be run
services:
# angular: # name of the first service
# hostname: localhost
# build: serviceangular # specify the directory of the Dockerfile
# ports:
# - 4200:4200 # specify port forewarding
#
express: #name of the second service
build: ./node-apis-project # specify the directory of the Dockerfile
ports:
- 3000:3000 #specify ports forwarding
links:
- database
database: # name of the third service
image: mongo # specify image to build container from
ports:
- 27017:27017 # specify port forewarding
volumes:
- ./data/mongo:/data/db
volumes:
data:
external: true
Docker在查看Express容器时记录输出
> apis-project@1.0.0 prod /usr/src/app
> npm run build && npm run start
> apis-project@1.0.0 build /usr/src/app
> tsc
> apis-project@1.0.0 start /usr/src/app
> nodemon ./dist/server.js
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): lib/**/*
[nodemon] watching extensions: js
[nodemon] starting `node ./dist/server.js`
(node:62) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:62) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Express server listening on port 3000
答案 0 :(得分:8)
您现在可能已经找到了解决方案。我将这个答案留作参考。
在docker-compose.yml
中,确保已将源代码映射到应用程序服务。就您而言:
version: '3'
services:
...
express:
build: ./node-apis-project
ports:
- 3000:3000
links:
- database
volumes:
- .:/usr/src/app # same as WORKDIR in your Dockerfile
...
根据nodemon manual,在某些联网环境中(例如运行nodemon的容器在已安装的驱动器上读取数据的情况下),常规监视机制可能会失败,因此您可能必须使用占用大量CPU资源的轮询(就我而言,CPU利用率为6-10%)
对于nodemon,请使用--legacy-watch
:
"dev3": "nodemon --legacy-watch --watch 'lib/*.ts' --exec 'ts-node' lib/server.ts",
对于ts-node-dev,请使用--poll
:
"dev2": "ts-node-dev --poll --respawn --transpileOnly ./lib/server.ts",
答案 1 :(得分:1)
对于由于 Kubernetes + ts-node-dev 失败并发送类似于以下内容的错误而遇到此问题的任何其他人:
[INFO] 09:29:47 ts-node-dev ver. 1.1.1 (using ts-node ver. 9.1.1, typescript ver. 4.1.3)
npm ERR! path /app
npm ERR! command failed
npm ERR! signal SIGKILL
npm ERR! command sh -c ts-node-dev src/index.ts
检查您在部署 yaml 中分配给容器的资源并尝试取消限制它们。
这为我解决了这个问题,我能够在此之后调整资源。