Devendra Verma
Devendra Verma

Reputation: 1015

Accessing files inside the folder, which is inside config folder in sails js(node js framework)

I have to access a file , which is inside a folder which is inside the config folder(/config/schemas/userSchemas.js). How to access this file from anywhere else like from my controllers or services. How to do this? I have tried sails.config.schemas.userSchemas. But it didn't work. What else I can try?

Upvotes: 1

Views: 406

Answers (3)

sgress454
sgress454

Reputation: 24948

It is really not recommended that you require configuration files directly. Sails will require them for you, and merge the contents into sails.config, ignoring your directory structure completely. So if, for example, your config/schemas/userSchemas.js file contains:

module.exports.userSchemas = {
   foo: "bar"
}

Then you'll be able to access that config in your app as sails.config.userSchemas. Note the first line very carefully: if you just do module.exports = {...} then the object will be merged into sails.config, not sails.config.userSchemas -- as I said before, configuration is merged in without regard to directory structure.

If you have files that you need to load in which you don't want to be part of sails.config, simply place them in a different folder in your project (e.g. schemas) and use require as specified in the other answers.

Upvotes: 1

suraj.tripathi
suraj.tripathi

Reputation: 467

I would suggest to define a global variable for path.

Consider you have following directory structure (as mentioned in @arif's answer).

myapp
├── app.js
├── _config
│   ├── _schemas
|       |
|       ├── userSchemas.js
|       ├── otherSchemas.js
|
├── _controller
│   ├── _module1
|   |   |
|   |   ├── userController.js
|   |   ├── otherController.js
|   ├── otherController.js

Now in you app.js write the first line as follow.

global.__base = __dirname + '/';

Note : now global.__base points to /path-to-your-myapp-directory/myapp/

Now you can access userSchema.js or any other file like this.

In all the files (app.js, otherController.js, userController.js), you can require userSchemas.js like this

require(global.__base + 'config/schemas/userSchemas.js');

Upvotes: 1

Arif Khan
Arif Khan

Reputation: 5069

Let's say you have following directory structure

myapp
├── app.js
├── _config
│   ├── _schemas
|       |
|       ├── userSchemas.js
|       ├── otherSchemas.js
|
├── _controller
│   ├── _module1
|   |   |
|   |   ├── userController.js
|   |   ├── otherController.js
|   ├── otherController.js

In app.js, you may access as

require('./config/schemas/userSchemas.js');

In otherController.js, you may access as

require('../config/schemas/userSchemas.js');

In userController.js, you may access as

require('../../config/schemas/userSchemas.js');

Conclude: .. is used to go to parent directory(one directory back) while . is used for current directory

I hope this will help you directory structure in node.js

Upvotes: 2

Related Questions