post-get-service
Version:
simple way to create node http in memory server
43 lines (33 loc) • 1.11 kB
JavaScript
const { common_template } = require('./common_template');
/**
* @param {object} jsom_model object
* @returns {string} return string with fastify plugin source code registering all routes
*/
function get_routing_model(jsom_model) {
const temp_model = JSON.stringify(jsom_model);
const top_base =
'async function router_worker (fastify, opts) {' +
'const fs = require("fs") \n' +
'const path = require("path") \n' +
'const querystring = require("querystring") \n' +
`// temp_model ${temp_model} \n`;
const bottom_base = '\n module.exports = router_worker; \n';
const { api } = jsom_model;
const live_check = `
\n
fastify.get('/', async (request, reply) => {
reply.code(200).send({ live: true });
return reply;
});
\n
`;
const routing_model = api.reduce((current_model, { path }) => {
const fastify_route_executor = common_template(path);
current_model += '\n' + fastify_route_executor;
return current_model;
}, '');
return top_base + routing_model + live_check + '\n}' + bottom_base;
}
module.exports = {
get_routing_model,
};