How to split a single Node.js file into separate m

2019-03-17 16:18发布

I want to split the code into different files. I currently write all get and post methods in the same file, but I want greater readability and manageability.

I've tried to put the code in different files, but while running the main app, the rest of get and post methods in other files are not able to called. I include this:

var Db = require('/filename.js'); // ...but I can't call those methods.

I want to split my single file code for readability. How do I achieve this?

标签: node.js npm
2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-17 16:33

Just have a look at the module documentation:

Starting with / looks for absolute paths, eg:

require('/home/user/module.js');

./ starts with the path where the calling file is located.

require('./lib/module.js');

__dirname has the same effect than ./:

require( __dirname + '/module.js');
查看更多
在下西门庆
3楼-- · 2019-03-17 16:40

Try:

var Db = require('./filename.js');

or

var Db = require('filename.js');

Also, have a look at this blog post.

查看更多
登录 后发表回答