1.总结
:
我创建了一个简单的应用程序,它使用Node。js、Express和Swagger,通过以下方式
tutorial
使用
generator-express-no-stress
.此外,申请书应与
pm2
在集装箱里。在我启动容器之前,一切都正常,在尝试读取
应用程序编程接口。亚马尔
.
2.步骤:
首先,我创建项目:
yo express-no-stress server
cd server
npm i
localhost
到
0.0.0.0
(
http.createServer(app).listen(port, '0.0.0.0', welcome(port));
),使用
npm run dev
然后呢
curl 0.0.0.0:3000/api/v1/spec
,它将返回(正如预期的那样)Swagger文件的规范。
接下来,我将pm2的配置文件添加到项目中,
pm2。json
:
{
"name": "server",
"script": "build/main.js",
"env": {
"APP_ID": "server",
"PORT": "3000",
"LOG_LEVEL": "debug",
"REQUEST_LIMIT": "100kb",
"SESSION_SECRET": "mySecret"
}
}
$ pm2 start pm2.json
$ curl 0.0.0.0:3000/api/v1/spec
$ pm2 delete all
再次返回规范。
接下来,我添加一个
Dockerfile
FROM node:alpine
COPY build build/
COPY package.json .
COPY pm2.json .
RUN npm i pm2 -g \
&& npm i --production \
&& npm cache verify
CMD ["pm2-runtime", "start", "pm2.json"]
我构建映像并运行容器:
docker build -t server:0.0.1 .
docker run --rm --net=host server:0.0.1
3.问题和疑问
这是我的问题:
[2018-05-30 09:51:49] PM2 log: Launching in no daemon mode
[2018-05-30 09:51:49] PM2 log: Starting execution sequence in -fork mode- for app name:server id:0
[2018-05-30 09:51:49] PM2 log: App name:server id:0 online
{"level":30,"time":1527673910123,"msg":"up and running in development @: r2d2 on port: 3000}","pid":17,"hostname":"r2d2","name":"server","v":1}
Error: Error opening file "///server/common/swagger/Api.yaml"
ENOENT: no such file or directory, open '///server/common/swagger/Api.yaml'
at ReadFileContext.callback (/node_modules/json-schema-ref-parser/lib/resolvers/file.js:52:20)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:352:13)
Error: ENOENT: no such file or directory, open '///server/common/swagger/Api.yaml'
我不知道为什么会出现错误,以及如何修复它。我不知道为什么要走这条路
///server/common/swagger/Api.yaml
看起来有点奇怪,我甚至不知道这是否会导致错误。尽管如此,容器仍在运行。那么:我该如何修复它,或者知道在哪里可以看到它?
4.进一步措施
但是,我已经完成了以下步骤来检查错误:
旋度0.0.0.0:3000/api/v1/spec
从我的主机返回:
<h1>500 Error</h1><pre>Error opening file "///server/common/swagger/Api.yaml"
ENOENT: no such file or directory, open '///server/common/swagger/Api.yaml'</pre>
带着
docker exec -it aacfcc6c5744 sh
:
pm2 show server
âââââââââââââââââââââ¬âââââââââââââââââââââââââââââââââââââ
| status â online â
â name â server â
â restarts â 0 â
â uptime â 3m â
â script path â /build/main.js â
â script args â N/A â
â error log path â /root/.pm2/logs/server-error-0.log â
â out log path â /root/.pm2/logs/server-out-0.log â
â pid path â /root/.pm2/pids/server-0.pid â
â interpreter â node â
â interpreter args â N/A â
â script id â 0 â
â exec cwd â / â
â exec mode â fork_mode â
â node.js version â 10.2.1 â
â watch & reload â â â
â unstable restarts â 0 â
â created at â 2018-05-30T09:51:49.825Z |
âââââââââââââââââââââ´âââââââââââââââââââââââââââââââââââââ
以及:
cd build/ && tree
.
âââ main.js
âââ main.map
âââ public
â  âââ api-explorer
â  â  âââ favicon-16x16.png
â  â  âââ favicon-32x32.png
â  â  âââ index.html
â  â  âââ oauth2-redirect.html
â  â  âââ swagger-ui-bundle.js
â  â  âââ swagger-ui-bundle.js.map
â  â  âââ swagger-ui-standalone-preset.js
â  â  âââ swagger-ui-standalone-preset.js.map
â  â  âââ swagger-ui.css
â  â  âââ swagger-ui.css.map
â  â  âââ swagger-ui.js
â  â  âââ swagger-ui.js.map
â  âââ index.html
âââ server
âââ common
âââ swagger
âââ Api.yaml
很抱歉问了这么长的问题,但我想确保我的所有步骤都清晰且可重复。如果你需要更多信息,请告诉我。
5.编辑
:
这个问题与pm2有关。如果输入容器,将应用程序的端口更改为3001(因为3000已经在使用中),然后执行以下操作:
npm start
(
cd build && node main
)它可以工作,我可以从我的本地机器访问规范。