1.9.54 • Published 5 months ago

ct-core v1.9.54

Weekly downloads
1
License
MIT
Repository
bitbucket
Last release
5 months ago

CT-CORE

npm.io

Build the REST API with json metadata.

This is a quick back-end system. You need create more modules, more versions, more APIs.. with only one json metadata

Requirements

Start MongoDB

mongod --port 27017

I. Getting started with global module

1. Install CT-CORE
npm install -g ct-core
2. Initial application
ct-core init
3. Updating application
ct-core update
4. Configuration environment
cp .env.copy .env
5. Start application
ct-core start

II. Getting started with include as a module

1. Init the project
npm init project-name

Open the file package.json and update tag "scripts"

"scripts": {
  "init": "cross-env START_WITH=init node index.js",
  "start": "cross-env START_WITH=start node index.js",
  "update": "cross-env START_WITH=update node index.js",
  "generate-doc": "cross-env START_WITH=apidoc node index.js",
  "clean-doc": "find . -type f -name '_doc.js' -delete",
  "apidoc": "npm run generate-doc && apidoc -i api/ -o doc/ && npm run clean-doc"
}

Add dependencies

npm install --save ct-core
npm install --save-dev apidoc
npm install --save-dev cross-env
2. Create index.js file
const core = require('ct-core');
const cmd = process.env.START_WITH ? process.env.START_WITH : 'start';
try {
  core(cmd);
} catch (e) {
  console.error(e);
}
3. Configuration environment
cp .env.copy .env
4. Start application as development
npm run start

Now go to homepage http://localhost:3000

III. Add connection to database

1. Connection the MongoDB

Create file: api/database/mongo.json

{
  "driver": "mongoose",
  "connection": "mongodb://{HOST}:{PORT}/{DATABASE}",
  "options": {
    "useNewUrlParser": true,
    "useUnifiedTopology": true,
    "serverSelectionTimeoutMS": 5000
  }
}

Add configuration to setting file: api/setting.json

"database": [
    {
      "MONGO_DB_HOST": "localhost",
      "MONGO_DB_PORT": "27017",
      "MONGO_DB_NAME": "eco",
      "MONGO_DB_USERNAME": "",
      "MONGO_DB_PASSWORD": ""
    }
  ],

2. Connection the MySQL or MariaDB

npm install --save mariadb

Create file: api/database/mariadb.json

{
  "driver": "mariadb",
  "database": "[DATABASE]",
  "config": {
    "connectionLimit" : 10,
    "host"            : "[SERVER]",
    "user"            : "[USERNAME]",
    "password"        : "[PASSWORD]",
    "database"        : "[DATABASE]"
  }
}

Add configuration to setting file: api/setting.json

"database": [
    {
      "MYSQL_DB_PORT": "3306",
      "MYSQL_DB_SERVER": "localhost",
      "MYSQL_DB_DATABASE": "eco",
      "MYSQL_DB_USERNAME": "eco",
      "MYSQL_DB_PASSWORD": "eco"
    }
  ],

3. Connection the SQL Server

npm install --save mssql

Create file: api/database/mssql.json

{
  "driver": "mssql",
  "database": "[DATABASE]",
  "connection": "Server=[SERVER],[PORT];Database=[DATABASE];User Id=[USERNAME];Password=[PASSWORD];",
  "config": {
    "user": "[USERNAME]",
    "password": "[PASSWORD]",
    "server": "[SERVER]",
    "database": "[DATABASE]",
    "connectionTimeout": 15000,
    "requestTimeout": 150000,
    "pool": {
      "min": 1,
      "max": 20,
      "idleTimeoutMillis": 30000
    },
    "options": {
      "port": "[PORT]",
      "appName": "node-mssql",
      "encrypt": false,
      "useUTC": true
    }
  }
}

Add configuration to setting file: api/setting.json

"database": [
    {
      "MSSQL_DB_PORT": "1433",
      "MSSQL_DB_SERVER": "10.0.0.15\\sql2012",
      "MSSQL_DB_DATABASE": "eco",
      "MSSQL_DB_USERNAME": "sa",
      "MSSQL_DB_PASSWORD": "2008"
    }
  ],

IV. How to test application

1. Output

HTTP/1.1 200 OK

[
  {
    "gender": "male",
    "_id": "5e7115bd926d13129e9b1cdf",
    "username": "username",
    "email": "email@domain.com",
    "password": "password",
    "age": 65,
    "phone": "0999999999",
    "birthday": "2020-03-17T07:06:16.664Z",
    "salary": 1500.8,
    "avatar": "https://i.pinimg.com/originals/90/b0/b3/90b0b3b15ccecf9efda592b0bdf462aa.jpg",
    "last_ip": "30.230.33.25"
  }
]

Also when doing requests, it's good to know that:

  • If you make POST, PUT, PATCH or DELETE requests, changes will be automatically.
  • Your request body JSON should be object enclosed, just like the GET output. (for example {"name": "Foobar"})
  • Id values are not mutable. Any id value in the body of your PUT or PATCH request will be ignored. Only a value set in a POST request will be respected, but only if not already taken.
  • A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will result in a 200 OK but without changes being made to the data.

2. Routes

    GET     -> /*/auth/get-token (*)
    GET     -> /*/auth/refresh-token (token)
    GET     -> /*/system/get-captcha (token)
    GET     -> /v1.0.0/setting/default (token)
    GET     -> /v1.0.0/setting/init/:password (token)
    GET     -> /v1.0.0/setting/get-by-id/:id (token,admin)
    PUT     -> /v1.0.0/setting/get-all (token,admin)
    POST    -> /v1.0.0/setting/add (token,admin)
    POST    -> /v1.0.0/setting/edit (token,admin)
    DELETE  -> /v1.0.0/setting/delete (token,admin)
    DELETE  -> /v1.0.0/setting/deletes (token,admin)
    POST    -> /v1.0.0/upload/file (token)
    PUT     -> /v1.0.0/user/login (token)
    PUT     -> /v1.0.0/user/logout (token)
    PUT     -> /v1.0.0/user/login-social (token)
    PUT     -> /v1.0.0/user/admin (token)
    PUT     -> /v1.0.0/user/check-username (token)
    PUT     -> /v1.0.0/user/check-email (token)
    PUT     -> /v1.0.0/user/forgot-password (token)
    PUT     -> /v1.0.0/user/reset-password (token)
    PUT     -> /v1.0.0/user/register-user (token)
    PUT     -> /v1.0.0/user/change-password (token,login)
    PUT     -> /v1.0.0/user/send-password (token,admin)
    POST    -> /v1.0.0/user/add-child (token,login)
    GET     -> /v1.0.0/user/get-children (token,login)
    DELETE  -> /v1.0.0/user/del-child (token,login)
    POST    -> /v1.0.0/user/add-user (token,admin)
    POST    -> /v1.0.0/user/edit-user (token,login)
    GET     -> /v1.0.0/user/get-by-id/:id (token,login)
    GET     -> /v1.0.0/user/get-all (token,admin)
    PUT     -> /v1.0.0/user/active-user (token,admin)
    PUT     -> /v1.0.0/user/disable (token,login)
    DELETE  -> /v1.0.0/user/delete-user (token,admin)
    DELETE  -> /v1.0.0/user/delete-users (token,admin)

V. CLI usage

1. ct-core init
2. ct-core update
3. ct-core apidoc
4. ct-core start [options]

Options:
  --port, -p     Start app with port
  --host, -h     Start app with host
  --help         Show help                  [boolean]
  --version, -v  Show version number        [boolean]

Examples:
  ct-core init
  ct-core update
  ct-core apidoc
  ct-core start -h localhost -p 3000

https://www.npmjs.com/package/ct-core

VI. Deployment

1. Install software to serve

npm install -g pm2
pm2 startup

2. Copy and edit the environment in the process.json

cp process.json.copy process.json
pm2 start process.json
pm2 save

VII. Build to binary

npm install -g pkg
npm run pack-linux | pack-mac | pack-win

License

MIT

1.9.54

5 months ago

1.9.53

7 months ago

1.9.52

8 months ago

1.9.51

8 months ago

1.9.49

9 months ago

1.9.48

9 months ago

1.9.47

9 months ago

1.9.50

9 months ago

1.9.46

1 year ago

1.9.45

1 year ago

1.9.44

1 year ago

1.9.43

1 year ago

1.9.42

1 year ago

1.9.41

1 year ago

1.9.40

2 years ago

1.9.39

2 years ago

1.9.29

2 years ago

1.9.28

2 years ago

1.9.27

2 years ago

1.9.26

2 years ago

1.9.25

2 years ago

1.9.24

2 years ago

1.9.23

2 years ago

1.9.38

2 years ago

1.9.37

2 years ago

1.9.36

2 years ago

1.9.35

2 years ago

1.9.34

2 years ago

1.9.33

2 years ago

1.9.32

2 years ago

1.9.31

2 years ago

1.9.30

2 years ago

1.9.22

2 years ago

1.9.21

2 years ago

1.9.20

2 years ago

1.9.19

2 years ago

1.9.18

2 years ago

1.9.17

2 years ago

1.9.16

2 years ago

1.9.15

2 years ago

1.9.14

2 years ago

1.9.13

2 years ago

1.9.12

2 years ago

1.9.11

2 years ago

1.9.10

2 years ago

1.9.9

2 years ago

1.9.8

2 years ago

1.9.7

2 years ago

1.9.6

3 years ago

1.9.5

3 years ago

1.9.4

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.8.9

3 years ago

1.8.8

3 years ago

1.8.7

3 years ago

1.8.6

3 years ago

1.8.5

3 years ago

1.6.4

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.7.9

3 years ago

1.7.8

3 years ago

1.7.7

3 years ago

1.7.6

3 years ago

1.7.5

3 years ago

1.7.4

3 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.7.10

3 years ago

1.7.11

3 years ago

1.7.12

3 years ago

1.7.13

3 years ago

1.7.14

3 years ago

1.7.15

3 years ago

1.7.16

3 years ago

1.7.17

3 years ago

1.7.18

3 years ago

1.7.19

3 years ago

1.7.20

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.5.38

3 years ago

1.5.37

3 years ago

1.5.39

3 years ago

1.6.11

3 years ago

1.6.10

3 years ago

1.6.12

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.8.4

3 years ago

1.8.3

3 years ago

1.5.34

3 years ago

1.5.36

3 years ago

1.5.35

3 years ago

1.5.30

3 years ago

1.5.32

3 years ago

1.5.31

3 years ago

1.5.33

3 years ago

1.5.25

4 years ago

1.5.27

3 years ago

1.5.26

3 years ago

1.5.29

3 years ago

1.5.28

3 years ago

1.5.24

4 years ago

1.5.23

4 years ago

1.5.22

4 years ago

1.5.21

4 years ago

1.5.18

4 years ago

1.5.19

4 years ago

1.5.20

4 years ago

1.5.17

4 years ago

1.5.16

4 years ago

1.5.15

4 years ago

1.5.14

4 years ago

1.5.12

4 years ago

1.5.13

4 years ago

1.5.11

4 years ago

1.5.10

4 years ago

1.5.9

4 years ago

1.5.8

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.3.30

4 years ago

1.4.0

4 years ago

1.3.29

4 years ago

1.3.28

4 years ago

1.3.27

4 years ago

1.3.26

4 years ago

1.3.25

5 years ago

1.3.24

5 years ago

1.3.23

5 years ago

1.3.22

5 years ago

1.3.21

5 years ago

1.3.19

5 years ago

1.3.20

5 years ago

1.3.17

5 years ago

1.3.18

5 years ago

1.3.16

5 years ago

1.3.14

5 years ago

1.3.15

5 years ago

1.3.13

5 years ago

1.3.11

5 years ago

1.3.12

5 years ago

1.3.9

5 years ago

1.3.10

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.30

5 years ago

1.2.29

5 years ago

1.2.27

5 years ago

1.2.28

5 years ago

1.2.25

5 years ago

1.2.26

5 years ago

1.2.24

5 years ago

1.2.23

5 years ago

1.2.22

5 years ago

1.2.21

5 years ago

1.2.16

5 years ago

1.2.17

5 years ago

1.2.18

5 years ago

1.2.19

5 years ago

1.2.20

5 years ago

1.2.13

5 years ago

1.2.14

5 years ago

1.2.15

5 years ago

1.2.12

5 years ago

1.2.11

5 years ago

1.2.10

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.1.17

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.2

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.21

5 years ago

0.0.22

5 years ago

0.0.20

5 years ago

0.0.17

5 years ago

0.0.18

5 years ago

0.0.19

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.10

5 years ago

0.0.11

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago