coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.81k stars 356 forks source link

Unable to connect mongodb server via mongoose #278

Closed JOFosuware closed 3 years ago

JOFosuware commented 3 years ago

When my node app was made to run as a service, Express server starts well and serves initial page. But in the same script where node server (Express) starts is a mongoose script called to connect mongodb. When I run the script with node commands from command prompt everything works fine but the app running as a service could not connect the database.

coreybutler commented 3 years ago

@JOFosuware - Does this apply to your situation? https://github.com/coreybutler/node-windows/wiki#my-script-runs-fine-on-its-own-but-not-with-node-windows-why

JOFosuware commented 3 years ago

Yes it applies, But help me with this, if I include a module like this 'require('./db/mongoose')' in my script, it is a relative path? My text editor pops up with full path.

coreybutler commented 3 years ago

Yes, require('./db/mongoose') is a relative path. It is relative to your project root. Most times this can be resolved by using __filename or process.cwd(), and sometimes path.resolve in your require statement (i.e. require(path.resolve('./db/mongoose'). Which of these options is correct for your needs depends on how you're running your script and from where.

If your app ships with the node_modules directory (most do by default), then require(path.join(process.cwd(), './db/mongoose')) is most likely the right option for you.

JOFosuware commented 3 years ago

Thanks, I will update my code.

JOFosuware commented 3 years ago

I have updated my script with both path.resolve() and have used path.join() with process.cwd() as you directed, the node server starts and serves pages but the mongodb cannot be connected by mongoose still. I will be grateful if helped.

tenzint commented 3 years ago

@JOFosuware hello. I also had similar problems with you. In my case, I noticed that the "mongoD.exe" was killed and "c/data/bin" directory was deleted. (I did restart my computer so it could be unrelated to node-windows). I had to rerun mongoD, create the directory, and seed my database for it to fix. I used path.resolve() to convert all the relative paths into absolute paths, while assuming "server" folder is where I run my service. I have other issues but that's unrelated here

JOFosuware commented 3 years ago

@tenzint I am glad you have shown me you have a shared problem. I have tried the solution provided by @coreybutler but it is not working for me. I hope @coreybutler is able to recognize the problem and come with a solution. I will be glad if you share your solution with me in case you find one.

tenzint commented 3 years ago

Hi @JOFosuware, my above comment shows my situation + my solution. Did you perhaps see a "ECONNREFUSE" or something similar for your MongoDB error when you run your backend service? Let me share you my experience, although it might be too verbose :) I initially used the path.resolve() to convert all my relative paths in the backend files to absolute paths. There is a little trick here that you probably noticed - you have to adjust the pathing to adjust to the fact that you are running the service from your "server" or "backend" folder. After changing all paths to absolute paths, I started running the service and then noticed the "ECONNREFUSE" error code (It could be a similar code, I forgot). Basically, this error code suggested it could not connect to MongoDB. I noticed now that even regular "npm start" command had the same issue. This was how I discovered "node-windows" killed MongoD.exe. So I ran "mongoD.exe" and created the "c/data/bin" directory. I suggest you to check if you have the same issues that I had. If it is the same, then my solution may help you. check for the error code and if it is different, then may be it could be solved via online

JOFosuware commented 3 years ago

@tenzint thanks a lot you for sharing your solution with me. You could help me more by showing me how the trick is done, " There is a little trick here that you probably noticed - you have to adjust the pathing to adjust to the fact that you are running the service from your "server" or "backend" folder." My root folder is /osee-pos, so my /src/db folder is within the root folder with my server.js file. This is how I generated the absolute path path.join(process.cwd(), '/src/db/mongoose.js') does it goes with the trick you are talking about.

tenzint commented 3 years ago

There is a little trick here that you probably noticed - you have to adjust the pathing to adjust to the fact that you are running the service from your "server" or "backend" folder.

I was talking about "path.resolve()" as that's what I used. This is a simple concept from: (path.resolve() Doc). My apologies if this reply is too long. The trick I was talking about is regarding the context shift of relative paths. I think you already know this and my wording was off earlier...

This only matters if you have nested directories in your backend's root folder ('/osee-pos' in your case; "server" in my case). Let's take my backend files as an example for my explanation. I have "/server" as the root of my backend. (I am ignoring the prefix of "c/username/projectname/" as that is irrelevant). Then I have folders "/server/routes", "/server/controllers", "/server/models" etc. I also have files in each of these folders. I am storing my "service.js" file that runs this "node-windows" script in "/server" folder. And I run the cmd terminal by going to the path of "/server" in cmd and running "node service.js". This forces the current directory of the service to be fixed to "/server". So when I use "path.resolve()", the current directory is "/server", and not where the file is located.

Suppose I am using a relative path in "/server/routes/index.js". For example, var controller = require("../controllers/helloController.js");. The current directory is assumed to be "/server/routes" generally. But for the service to work, the current directory is changed to "/server" (this was decided by running "node service.js" from path "/server" in cmd. Therefore, if we were to change the code to var controller = require(path.resolve("../controllers/helloController.js"));, you will get an error saying the path "/../controllers" doesn't exist ("/../" means the parent of "/server"). So this is what I meant by the "trick" - You have to adjust the code to var controller = require(path.resolve("controllers/helloController.js"));. Similarly, if you are referencing to "/server/routes/job.js" from "/server/routes/index.js", then your code will be converted to var jobRoute = require(path.resolve("routes", "./job.js"));.

I think "path.join(process.cwd()...)" could be similar to "path.resolve()", but you can decide that after reading your docs. My advice is to check for all backend files if you are using any relative paths in your code and convert them to absolute paths using the above example or its equivalent. There is only a need for a little context shift.

I don't know how you plan to run your service script file - that will determine your current directory for the service. Suppose you save your "service.js" file in your "/osee-pos" root folder and run your "node service" from this root folder. Then ALL of the paths should be joined from "/osee-pos". If you are calling "/osee-pos/src/db/mongoose.js" file from "/osee-pos/src/controllers/userController.js" and the initial code was var db = require("../db/mongoose.js");, then the new code will be var db = require(path.resolve("src/db/mongoose.js"));.

JOFosuware commented 3 years ago

Thanks @tenzint for coming down like that, my is working now.

tenzint commented 3 years ago

Glad to help out!