const koa = require('koa'), app = new koa(), Router = require('koa-router'), router = new Router(), host = '127.0.0.1', port = 12899, jsonwebtoken = require('jsonwebtoken'), jwt = require('koa-jwt'); router .post('/users/', (ctx, next) => { ctx.body = 'add users'; }) .del('/users/:id', (ctx, next) => { ctx.body = 'remove users'; }) .put('/users/:id', (ctx, next) => { ctx.body = 'modify users'; }) .get('/users/:id', (ctx, next) => { ctx.body = 'retrieve users'; }); app.use(router.routes()); app.listen(port, host, function () { console.log(`server is running in http://${host}:${port}`); });
const koa = require('koa'), app = new koa(), Router = require('koa-router'), router = new Router({ prefix : '/user' }), host = '127.0.0.1', port = 12899, jsonwebtoken = require('jsonwebtoken'), jwt = require('koa-jwt'); // 那個路由在前面就先匹配那個路由 router .get('/:id/:level', async (ctx, next) => { ctx.body = `get start: ${ctx.params.id}--${ctx.params.level}==>`; await next(); //必須加這個才匹配 all }, async (ctx, next) => {//中間件 ctx.body += ' second mid'; await next(); }) .all('/', (ctx, next) => { ctx.body += ' all info'; // 設置跨域請求,跨域資源共享CORS(*指所有) ctx.set('Access-Control-Allow-Origin', '*'); }); app.use(router.routes()); app.listen(port, host, function () { console.log(`server is running in http://${host}:${port}`); });