Answers for "example route in koa and controller"

0

example route in koa and controller

// user.controoler.js
exports.resultsController =  async(ctx, next) => {
    ctx.body = "Hello Wordl from get";
    await next();
}

exports.createController = async (ctx, next) => {
    ctx.body = JSON.stringify(ctx.request.body);
    await next();
}

=================================

// user.route.js
const {resultsController, createController} = require("../controllers/user.controller");
const Router = require("koa-router");
const router = new Router();

router.get("/", resultsController);
router.post("/", createController);

module.exports = router.routes();

=================================
// index.js

const koa = require("koa");
const app = new koa();
const server = require("http").createServer(app.callback());
const bodyParser = require("koa-body");
const logger = require("koa-logger");

// iinit all route 
const userRoute = require("./routes/user.route");

// register all function middleware
app.use(bodyParser());
app.use(logger());
app.use(userRoute);

// listeing server
server.listen(3000, () => console.log("server is running"));
Posted by: Guest on May-05-2020

Code answers related to "example route in koa and controller"

Code answers related to "Javascript"

Browse Popular Code Answers by Language