• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ria-com/node-koajs-rest-skeleton: A simple Koajs 2 Application REST Skeleton

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

ria-com/node-koajs-rest-skeleton

开源软件地址:

https://github.com/ria-com/node-koajs-rest-skeleton

开源编程语言:

JavaScript 95.8%

开源软件介绍:

node-koajs-rest-skeleton v3.5.3

A simple Koajs 2.7.0 Application REST Skeleton This version based on koa 2.7.0.

quick start

Checkout node-koajs-rest-skeleton:

$ git clone https://github.com/ria-com/node-koajs-rest-skeleton

Make your own new project:

$ mv node-koajs-rest-skeleton my_new_project
$ cd my_new_project
$ rm -rf .git

Edit package.json:

$ vi package.json

Edit config/ files:*

$ vi config/default.js

Install modules

$ npm install

Start app:

$ node ./index.js

testing

Make your own Spec-files for testing and start test

In this skeleton for automatic testing was used jasmine-nodie & Frisby (REST API testing framework). I wrote several tests that you can use as examples.

All tests should be placed in the ./spec folder. The name of each test file must end with *Spec.js

To run the tests, use

$ cd my_new_project
$ npm test

Manual testing your REST service:

You can also manual check the serviceability of your service with bash and curl

get user id 1
$ curl -XGET "http://localhost:8081/users/1"
get all users
$ curl -XGET "http://localhost:8081/users"
add new user
$ curl -XPOST "http://localhost:8081/users" -d '{"name":"New record 1"}' -H 'Content-Type: application/json'
edit user id 3
$ curl -XPUT "http://localhost:8081/users/3" -d '{"name":"New record 3"}' -H 'Content-Type: application/json'
delete user id 3
$ curl -XDELETE "http://localhost:8081/users/3"

console api

Usage: /usr/bin/node ./console.js --section [string] [--action [string]] [--opt [object]]

Options:
  --opt, --options  example --opt.app=mobile --opt.s=1  [default: {}]
  --section                                             [required]
  --action                                              [default: "index"]

For example

$ ./console.js --section=default --opt.hello=world
Hello world defaultController & index action with options: {"hello":"world"}

rabbitmq api

Usage: NODE_WORKER_NAME=[worker_name] NODE_QUEUE_NAME=[queue_name] /usr/bin/node --harmony ./worker.js

For example

$ NODE_WORKER_NAME=example NODE_QUEUE_NAME=example /usr/bin/node --harmony ./worker.js

kubernetes api

Several new features have been added that can be used in conjunction with kubernetes

  • Auto shutdown. Set the environment variable NODE_LIFE_TIME to specify the time at which the service will suspend its work, for exsmple: NODE_LIFE_TIME=24h или NODE_LIFE_TIME=30m

    If the variable is not set, then "Auto shutdown" is disabled

  • Redy state. Your app can tell the kubernetes system that it is temporarily not ready to accept new requests. How to do this is described in the example below

       const {setReady} = require('../controllers/kubernetesController');
       // ...
       // setReady(false) // to temporary disable new requests
       // ...
       // setReady(true) // to restore accept new requests

    This should be configured in the config of kubernetes pod, the address on which poll is created: /redyz

  • Health state. Your app can tell the kubernetes system that it is temporarry broken. How to do this is described in the example below

       const {setHealth} = require('../controllers/kubernetesController');
       // ...
       // setHealth(false) // to tell kubernetes: "app is broken" 
       // ...
       // setHealth(true) // to tell kubernetes: "app is live"

    This should be configured in the config of kubernetes pod, the address on which poll is created: /healthz

In order to avoid cluttering the minimal code of our REST-service, additional functionality will be available when running the app via index.kubernetes.js:

$ node ./index.kubernetes.js

history

  • v3.5 - Joi validator was added to check PUT & POST input data (thanks to Roman Yakovenko)
  • v3.4 - Fix Dockerfile add /version route to kubernetes version
  • v3.3 - Some kubernetes features added
  • v3.2 - Updated dependencies for Koa 2.5, fix api tests, remove .babelrc



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap