This template provides Node.js 12 (LTS) and full access to koa for building microservices for OpenFaaS, Docker, Knative and Cloud Run.
With this template you can create a new microservice and deploy it to a platform like OpenFaaS for:
- scale-to-zero
- horizontal scale-out
- metrics & logs
- automated health-checks
- sane Kubernetes defaults like running as a non-root user
This template is experimental and I would like your feedback through GitHub issues.
- x86_64
You can create or scaffold a new microservice using the OpenFaaS CLI.
# USERNAME is your Docker Hub account or private Docker registry
$ export USERNAME=phumberdroz
$ faas template pull https://github.com/phumberdroz/openfaas-node12-koa-service
$ faas new --lang node12-koa-service microservice1 --prefix="${USERNAME}"
Once you've written your code you can run faas-cli build
to create a local Docker image, then faas-cli push
to transfer it to your registry.
You can now deploy it to OpenFaaS, Knative, Google Cloud Run or even use docker run
.
See also: Deploy OpenFaaS
const Router = require('@koa/router');
const bodyParser = require('koa-bodyparser');
module.exports = async (config) => {
const { app } = config;
app.use(bodyParser());
const router = new Router();
router.get('/test', (ctx, next) => {
// ctx.router available
ctx.body = {test: "success"}
});
app
.use(router.routes());
};
npm install --save moment
const Router = require('@koa/router');
const bodyParser = require('koa-bodyparser');
const moment = require('moment');
module.exports = async (config) => {
const { app } = config;
app.use(bodyParser());
const router = new Router();
router.get('/test', (ctx, next) => {
// ctx.router available
ctx.body = {time: moment()}
});
app
.use(router.routes());
};
const Router = require('@koa/router');
const bodyParser = require('koa-bodyparser');
module.exports = async (config) => {
const { app } = config;
app.use(bodyParser());
const router = new Router();
router.use((ctx, next) => {
ctx.state.route = ctx._matchedRoute;
return next();
})
router.get('/test', (ctx, next) => {
// ctx.router available
ctx.body = {time: moment()}
});
router.post('/test', (ctx, next) => {
ctx.body = ctx.request.body;
})
app
.use(router.routes());