How to get the project root directory in koa2

_ _ dirname is the current directory, not the project root.

the initial question is that I want to put all the uploaded files in the upload directory under the root directory of the project, but how do I get this root directory?

consider storing _ _ dirname in the properties of app in app.js in the root directory, but how to access app in subsequent middleware? Some middleware can wrap app when calling, such as router (app), but some don"t, only ctx. Can you access app from ctx?

Thank you.

Jun.20,2022

process.cwd () gets the root directory where node works, and files can be obtained through relative paths.

Menu