When I'm developing, I can connect to cosmodb no problem, but when I deploy it to azure app service, the log stream in app service says that the cosmosdb connection failed. I'm using environment variables so I figured that was the problem espcially since azure didn't copy over the .env file to site/wwwroot so I went to application settings and set the values for each env variable required to connect, but it still fails.
As an experiment, I also tried using a hardcoded connection string to mongodb atlas that works locally, but that failed too when deployed to app service. Maybe it's the port in server.js that's the issue. First time deploying to cloud so I'm a noob. I appreciate any help with this!
020-02-20T23:37:00.887948933Z: [INFO] 2020-02-20T23:37:00: PM2 log: Use `pm2 show <id|name>` to get more details about an app
2020-02-20T23:37:00.888703339Z: [INFO] 2020-02-20T23:37:00: PM2 log: [--no-daemon] Continue to stream logs
2020-02-20T23:37:00.897860809Z: [INFO] 2020-02-20T23:37:00: PM2 log: [--no-daemon] Exit on target PM2 exit pid=58
2020-02-20T23:37:06.517883906Z: [INFO] 23:37:06 0|server | BLOB/home/site/wwwroot
2020-02-20T23:37:07.340932229Z: [INFO] 23:37:07 0|server | (node:69) 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.
2020-02-20T23:37:07.529169475Z: [INFO] 23:37:07 0|server | (node:69) 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.
2020-02-20T23:37:07.912008216Z: [INFO] 23:37:07 0|server | Connection on Failed
server.js
const app = require("./app");
const debug = require("debug")("node-angular");
const http = require("http");
const mongoose = require("mongoose");
var redis = require("redis");
var env = require("dotenv").config();
const normalizePort = val => {
var port = parseInt(val, 10);
if (isNaN(port)) {
e;
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
};
const onError = error => {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
mongoose
.connect(
"mongodb://" +
process.env.COSMOSDB_HOST +
":" +
process.env.COSMOSDB_PORT +
"/" +
process.env.COSMOSDB_DBNAME +
"?ssl=true&replicaSet=globaldb",
{
auth: {
user: process.env.COSMOSDB_USER,
password: process.env.COSMOSDB_PASSWORD
}
}
)
.then(() => console.log("Connection to CosmosDB successful"))
.catch(err => console.error(err));
const onListening = () => {
const addr = server.address();
const bind = typeof port === "string" ? "pipe " + port : "port " + port;
debug("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", process.env.PORT || port);
var server = app.listen(app.get("port"), function() {
debug("Express server listening on port " + server.address().port);
});