Devendra Verma Devendra Verma - 2 months ago 6
Node.js Question

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?

Answer

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