Skip to content

Commit

Permalink
Merge pull request #8 from gotocva/v1.1.0
Browse files Browse the repository at this point in the history
💎 new stable version release
  • Loading branch information
gotocva authored Dec 6, 2023
2 parents 44394bd + 370cebb commit 9104395
Show file tree
Hide file tree
Showing 16 changed files with 479 additions and 379 deletions.
173 changes: 41 additions & 132 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,156 +44,65 @@ node nila create:model <modelName>
### Directory structure

```bash
[-] src
|-- config
| |-- app.config.js
| |-- db.config.js
|-- controllers
| |-- user.controller.js
|-- database
| |-- mongoose.db.js
|-- logger
| |-- index.js
| |-- pm2-viewer.js
|-- middlewares
| |-- auth.middleware.js
| |-- objectid.middleware.js
|-- models
| |-- user.model.js
|-- routes
| |-- index.js
| |-- user.routes.js
|-- utils
| |-- email.util.js
| |-- encryption.js
| |-- response.js
| |-- xss.js
|-- validators
| |-- user.validator.js
|-- express.js
|-- [-] app
| |-- index.js
|-- [-] config
| |-- mongoose.js
|-- [-] cron
| |-- index.js
|-- [-] modules
| |-- admin
| |-- admin.controller.js
| |-- admin.model.js
| |-- admin.routes.js
|-- [-] public
|-- [-] swagger
| |-- docs
| |-- index.js
|-- [-] test
| |-- admin.test.js
| |-- app.test.js
|-- [-] utils
| |-- crypto.js
| |-- logs.js
| |-- params-validator.js
| |-- response.handler.js
|-- [-] views
| |-- index.pug
| |-- layout.pug
|-- index.js
[-] tests
- .babelrc
- .env-sample
- .eslintrc
- .gitignore
- ecosystem.config.json
- jest.config.js
- jsconfig.json
- nila
- package-lock.json
- package.json
- README.md
|- .env.example
|- .eslintrc
|- ecosystem.config.json
|- jest.config.js
|- package-lock.json
|- package.json
|- README.md
```

Let's go through each folder and its purpose:

1. src: This is the main folder that contains the source code of the application.

1. config: This folder holds configuration files, such as database configurations, environment variables, logging settings, etc.

2. controllers: This folder contains the controllers responsible for handling HTTP requests, processing data, and interacting with services.

3. models: This folder houses the data models or schema definitions for your application's data layer.

4. routes: This folder contains the route definitions for different endpoints of your API or application.

5. services: This folder holds the business logic or services that are responsible for processing data, interacting with models, and performing application-specific operations.

6. utils: This folder contains utility files and helper functions that can be used across different parts of your application.

2. tests: This folder is dedicated to storing your application's unit tests, integration tests, or any other test files.

3. package.json: This file defines your project's metadata, dependencies, and scripts.

4. .env: This file is used to store environment-specific configuration variables. It is usually not committed to version control and can contain sensitive information like API keys or database credentials.

5. index.js: This file serves as the entry point of your application. It typically sets up the server, establishes database connections, and initializes other essential components.
```Coming soon```

# Configuration

Configure the application and database details in ```.env``` or ```config``` files


> **Note**:
Configuration values configured in ```.env``` has the first priority, if you need to use the config.js values then skip those keys on ```.env``` file
Configure the application and database details in ```.env```file



```bash title=".env"
NODE_ENV=dev

PORT=PORT_TO_RUN


SOCKET_CORS_ORIGIN='http://localhost:3000'

# URL of the Mongo DB
MONGODB_URI="CONNECTION_STRING"

SEARCH_API_KEY=GOOGLE_SEARCH_API_KEY
PORT=8000

TELEGRAM_BOT_TOKEN="TELEGRAM_BOT_TOKEN"
MONGODB_URL="mongodb://localhost:27017/sparkportal"

# SMTP credentials
[email protected]
SMTP_HOST='smtp.zoho.com'
SMTP_PORT=465
[email protected]
SMTP_PASSWORD=
JWT_SECRET='nilajs'

PM2_LOGS_DIRECTORY='/Users/siva/.pm2/logs/npm-start-out.log'
PM2_LOGS_USERNAME='admin'
PM2_LOGS_PASSWORD='admin'

```

```
|-src
|--config
|---app.config.js
|---db.config.js
```


```js title="app.config.js"
export const appConfig = {
// application name inside the project
NAME : env.APP_NAME || 'Application name',

NODE_ENV : env.NODE_ENV || 'development',
PORT : env.PORT || '8080',

SMTP_EMAIL_FROM : env.SMTP_EMAIL_FROM || '[email protected]',
SMTP_HOST : env.SMTP_HOST || 'smtp.zoho.com',
SMTP_PORT : env.SMTP_PORT || 465,
SMTP_USERNAME : env.SMTP_USERNAME || '[email protected]',
SMTP_PASSWORD : env.SMTP_PASSWORD || '',


TELEGRAM_BOT_TOKEN : env.TELEGRAM_BOT_TOKEN || '',

API_RATE_LIMIT_TIME : 15, // 15 minutes
API_RATE_LIMIT : 100, // 100 requests for every 15 minutes

LOG_DIRECTORY: 'logs',

PM2_LOGS_DIRECTORY : env.PM2_LOGS_DIRECTORY || '/Users/siva/.pm2/logs/npm-start-out.log',
PM2_LOGS_USERNAME: env.PM2_LOGS_USERNAME || 'admin',
PM2_LOGS_PASSWORD: env.PM2_LOGS_PASSWORD || 'admin',
}
```
BCRYPT_SALT_ROUND=10

```js title="db.config.js"
export const dbConfig = {

MONGODB_URI : env.MONGODB_URI || '',
SWAGGER_USERNAME=admin
SWAGGER_PASSWORD=admin

CONNECTION_OPTIONS : {
useNewUrlParser: true,
useUnifiedTopology: true
}
}
```

```
Expand Down
134 changes: 81 additions & 53 deletions boilerplate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,106 @@

## NilaJS REST api framework

- src
- config
- app.config.js (application configuration files)
- db.config.js (database configuration files)
- controllers
- index.js (controller files)
- middlewares
- index.js (middleware files)
- models
- user.model.js ( User model )
- index.js (model files)
- routes
- index.js (route files)
- services
- index.js (service files)
- utils
- index.js (utility files)
- validators
- user.validator.js (validation files)
- tests
- package.json
- .env
- .env-sample
- jest.config.js
- jsconfig.json
- .eslintrc
- .babelrc (babel configuration file)
- index.js (entry point)

```bash
npm i nilajs -g
```

Let's go through each folder and its purpose:

1. src: This is the main folder that contains the source code of the application.

1. config: This folder holds configuration files, such as database configurations, environment variables, logging settings, etc.
```bash
nila create:app <appName>
```

2. controllers: This folder contains the controllers responsible for handling HTTP requests, processing data, and interacting with services.
Steps to run the application REST api

3. models: This folder houses the data models or schema definitions for your application's data layer.
```bash
cd <appName>
npm install
npm start
```

4. routes: This folder contains the route definitions for different endpoints of your API or application.
To use nila cli to generate files

5. services: This folder holds the business logic or services that are responsible for processing data, interacting with models, and performing application-specific operations.
Run below command to generate a new module with basic CRUD operations

6. utils: This folder contains utility files and helper functions that can be used across different parts of your application.

2. tests: This folder is dedicated to storing your application's unit tests, integration tests, or any other test files.
```bash
nila create:module <moduleName>
```

3. package.json: This file defines your project's metadata, dependencies, and scripts.
Run below command to generate a new controller

4. .env: This file is used to store environment-specific configuration variables. It is usually not committed to version control and can contain sensitive information like API keys or database credentials.
```bash
node nila create:controller <controllerName>
```

5. index.js: This file serves as the entry point of your application. It typically sets up the server, establishes database connections, and initializes other essential components.
Run below command to generate a new model

Steps to run the application REST api
```bash
node nila create:model <modelName>
```
npm install
npm start

### Directory structure

```bash
|-- [-] app
| |-- index.js
|-- [-] config
| |-- mongoose.js
|-- [-] cron
| |-- index.js
|-- [-] modules
| |-- admin
| |-- admin.controller.js
| |-- admin.model.js
| |-- admin.routes.js
|-- [-] public
|-- [-] swagger
| |-- docs
| |-- index.js
|-- [-] test
| |-- admin.test.js
| |-- app.test.js
|-- [-] utils
| |-- crypto.js
| |-- logs.js
| |-- params-validator.js
| |-- response.handler.js
|-- [-] views
| |-- index.pug
| |-- layout.pug
|-- index.js
|- .env.example
|- .eslintrc
|- ecosystem.config.json
|- jest.config.js
|- package-lock.json
|- package.json
|- README.md
```

To use nila cli to generate files
Let's go through each folder and its purpose:

Run below command to generate a new controller
```Coming soon```

```
node nila create:controller <controllerName>
```
# Configuration

Run below command to generate a new model
Configure the application and database details in ```.env```file



```bash title=".env"

PORT=8000

MONGODB_URL="mongodb://localhost:27017/sparkportal"

JWT_SECRET='nilajs'

BCRYPT_SALT_ROUND=10

SWAGGER_USERNAME=admin
SWAGGER_PASSWORD=admin

```
node nila create:model <modelName>
```

```
Expand Down
10 changes: 10 additions & 0 deletions boilerplate/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ app.use(express.json());
// Middleware to handle form-urlencoded data
app.use(express.urlencoded({ extended: true }));

app.set('view engine', 'pug');

// Serve static files from the 'public' directory
app.use(express.static('public'));

const auth = basicAuth({
users: { [env.SWAGGER_USERNAME || 'admin']: env.SWAGGER_PASSWORD || 'admin' },
challenge: true,
Expand All @@ -30,6 +35,11 @@ const swaggerInit = async () => {
}
swaggerInit();

app.get('/', (req, res) => {
const users = [{id: 1, name: 'siva'}]
res.render('index', { users });
});

// import module routes
const adminRouter = require('../modules/admin/admin.routes');

Expand Down
1 change: 1 addition & 0 deletions boilerplate/modules/admin/admin.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const login = async (req, res) => {
return sendErrorResponse(res, { message : 'incorrect password' , statusCode: 400 });
}
} else {

return sendErrorResponse(res, { message : 'Email not exists' , statusCode: 400 });
}

Expand Down
Loading

0 comments on commit 9104395

Please sign in to comment.