最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

NPM

旗下网站admin45浏览0评论

NPM

NPM

NPM - 应用程序根目录的路径(NPM - path to root of the application)

我正在开发一个NPM包,它需要从应用程序根目录中获取一个模块。 将其视为需要属性模块的包,该模块由用户放置在应用程序根目录中。

如何获取应用程序根目录的路径?

更新:

总结一下我要做的事情,express允许你做这样的事情:

app.use(express.router(myNPMModule.router));

路由器功能将获取app作为参数。 这个想法是允许用户在单独的文件中指定路由,他们只需要遵循某些约定,例如将他们的控制器放在/controllers文件夹中,我的模块将能够动态地解析路径,然后调用正确的正确模块中的方法。 我已经有了它的工作,但只是意识到,如果我将它打包在NPM中,我就无法访问应用程序的路径,所以我无法动态调用控制器模块的方法。

I am developing a NPM package, which needs to require a module from the application root. Think of it as the package requiring a properties module, which is placed in the application root by the user.

How do I get the path to root of the application?

UPDATE:

To summarize what I am trying to do, express allows you to do something like this:

app.use(express.router(myNPMModule.router));

The router function will get app as the parameter. The idea is to allow the users to specify the routes in a separate file, and they just need to follow certain conventions like putting their controllers in /controllers folder, and my module will be able to dynamically parse the path, and then invoke the correct method in the correct module. I have already got it working, but just realized that if I package it in NPM, I no longer have access to the path of the app, so I can't invoke a controller module's method dynamically.

最满意答案

让用户调用一个设置变量的函数:

myModule.setProps(json) OR myModule = new MyModule(json)

然后,用户可以要求他们的根配置并使用它来启动您的模块。

更新回复编辑过的问题:

Express允许您定义如下路线:

var routes = require('./routes')app.get('/info', routes.info);//Routes Moduleexports.info = function(req, res) { res.render('info', {title: 'Express'})};

我相信这正是你想要做的。 不是吗?

更新2:

您可以使用__dirname找到当前正在执行的脚本所在的目录

console.log(__dirname);

Make the user call a function that sets the variables:

myModule.setProps(json) OR myModule = new MyModule(json)

Then the user can require their root config and init your module with it.

Update In response to edited question:

Express allows you to define routes like this:

var routes = require('./routes')app.get('/info', routes.info);//Routes Moduleexports.info = function(req, res) { res.render('info', {title: 'Express'})};

I believe this is exactly what you are trying to do. Is it not?

Update 2:

You can find the directory the currently executing script is in using __dirname

console.log(__dirname);

发布评论

评论列表(0)

  1. 暂无评论