gpt4 book ai didi

javascript - docker-compose 仅​​运行我的 mongodb 服务器但不运行我的 js 文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:10 25 4
gpt4 key购买 nike

我刚开始使用 docker。我正在开发一个简单的表单,它将用户输入的数据提交到 mongodb 数据库中,并将他/她重定向到感谢 html 页面。它在我的本地主机上运行得很好,但是当我对其进行 dockerize 时,它​​不起作用。问题是,我的代码在 Mac 上运行得很好,但在 Windows 操作系统上却不行。任何帮助将不胜感激。

这是我的js代码:

var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var MongoClient = require('mongodb').MongoClient;

app.use(express.static(__dirname + '/../public'));
app.get('/', (req,res)=> file = fs.createReadStream(__dirname + '/clientlogin.html').pipe(res));
app.get('/thanks', (req,res)=> file = fs.createReadStream(__dirname + '/formthanks.html').pipe(res));
app.post('/', urlencodedParser, function(req,res){
var firstname = req.body.firstname;
var lastname = req.body.lastname;
var email = req.body.email;
var phonenumber = req.body.phonenumber;
var pass = req.body.password;

// Use connect method to connect to the server
var url = "mongodb://mongo:27017/clientdb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var obj = {
'FirstName': firstname,
'LastName': lastname,
'Email': email,
'PhoneNumber': phonenumber,
'Pass': pass
};
db.collection("myclients").insertOne(obj, function(err, res) {
if (err) throw err;
console.log("1 client registered!");
db.close();
});
res.redirect('/thanks');
});

});
app.listen(8081, function(){
console.log("Listening on port 8081");
});

我的package.json代码

{
"name": "loginpage",
"version": "1.0.0",
"description": "login page",
"main": "signupmongo.js",
"scripts": {
"start": "node signupmongo.js"
},
"author": "David",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"fs": "0.0.1-security",
"mongodb": "^2.2.33",
"mongoose": "^4.13.4"
}
}

我的dockerfile

FROM node:argon
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8081
CMD ["npm", "start"]

我的 docker-compose.yml 文件

version: "3"
services:
web:
build: .
volumes:
- ./:/app
ports:
- "8081:8081"
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"

这是代码似乎停止运行的命令行界面。 enter image description here

最佳答案

您必须在应用程序容器中链接 mongo 容器。为此使用链接功能示例:

version: "3"
services:
web:
build: .
volumes:
- ./:/app
ports:
- "8081:8081"
links:
- mongo
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"

关于javascript - docker-compose 仅​​运行我的 mongodb 服务器但不运行我的 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47459694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com