解决方案:可以使用多个koa-static中间件实例,并为每个实例指定不同的根目录和访问路径。例如: javascript app.use(serve('path/to/first/static/dir', { prefix: '/static1' })); app.use(serve('path/to/second/static/dir', { prefix: '/static2' })); 在这个例子中,访问http://localhost:3000/st...
var path=require('path') var static=require('koa-static') app.use(static(path.join(__dirname,'public'))) router.use('/',(ctx)=>{ ctx.body='koa' }) app.use(router.routes()) .use(router.allowedMethods()) app.listen(3000,()=>{ console.log('服务器已启动,端口正在监听...') })...
var path=require('path') var static=require('koa-static') app.use(static(path.join(__dirname,'public'))) var render=require('koa-art-template') render('app',{ root:path.join(__dirname,'views'), extname:'.html', debug:procress.env.NODE_ENV!=='productions' }) router.use('/',asy...
// 1 引入路由constkoaRouter =require("@koa/router");// 2 创建路由对象constuserRouter =newkoaRouter({prefix:"/users"});//后面所有的地址都会拼上 /users这个前缀// 3 注册各种中间件,区分请求方式和路径userRouter.get("/",(ctx, next) =>{ ctx.body="users list data~"; }); userRouter.ge...
const router = new Router({ prefix: '/user' })复制代码 1. 请求方式 ; koa-router 支持常用的请求方式, 推荐大家使用 get post put delete patch head options // 示例router.get(...)复制代码 1. 中间件注册 ; 上述示例中可以看出, 在app.use 之前 router 和 koa 还是没有关联的, 如果想要他们关...
Koa-static是用来管理静态资源目录的中间件,它也是上传文件等必不可少的中间件。 koa-redisconstredisStore=require('koa-redis') 说到Redis的话大家应该都不陌生,相对于Mysql硬盘数据库来说的内存数据库。在很多地方我们需要做缓存的就会用到该插件了。
3、node的路由koa-router const router = new Router();//app--->application const usersRouter = new Router({prefix:'/users'}); const orderRouter = new Router({prefix:'/order'}); router.get('/',(ctx)=>{ ctx.body = 'ssss'
static koa-static。 conststatic=(dirname)=>async(ctx,next)=>{letpathname=path.join(dirname,ctx.path);try{letstatObj=awaitfs.stat(pathname);if(statObj.isDirectory()){pathname=path.join(pathname,'index.html');}awaitfs.access(pathname);ctx.body=awaitfs.readFile(pathname,'utf-8');}catch(e...
letrouter=newRouter({prefix:'/users'}) url上的参数是生成在了ctx.params里,比如 router.get('/:category/:title',function(ctx,next){ctx.params// {category: 'programing', title: 'how-to-koa'}}) HTTP URL上GET常用的参数传递方式是queryString,比如home?id=12&name=foobar ...
const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); // 设置路由前缀 const router = new Router({ prefix:'/project' }) router.get('/', (ctx) => { ctx.body = 'Hello Koa'; }) .get('/todo', (ctx) => { ctx.body = 'Todo page'; ...