nodejs 分页

view ---person.html

  

  
    
    
    
    Bootstrap 101 Template

    
    

    
    
    
  
  
    
{% for per in persons %} {% endfor %}
id name age
{{per.id}} {{per.name}} {{per.age}}

服务器部分

var config = {
    port: 3000,
    staticPath: './bower_components',
    viewPath: './views'
}
var koa = require('koa');
var router = require('koa-router')();
var views = require('co-views');
var staticServe = require('koa-static');
var logger=require('koa-logger')
var render = views(config.viewPath, {
    map: { 'html': 'swig' }
});

var app = koa();

router.get('/person', function*(){
    var persons = [
            {
                id: 1,
                name: 'aaa',
                age: 18
            },
            {
                id: 2,
                name: 'vvv',
                age: 18
            },
            {
                id: 3,
                name: 'ccc',
                age: 18
            },
            {
                id: 4,
                name: 'iii',
                age: 18
            },
            {
                id: 5,
                name: 'ppp',
                age: 18
            },   
            {
                id: 6,
                name: ';;;',
                age: 18
            },
        ]
    
    var query = this.request.query;

    var page = query.page;
    var count = 3;
    if(!page){
        page=1;
    }

    persons = persons.slice( (page-1)*count, page*count);


    var context = {
        persons: persons    
    }
    this.body = yield render('person.html', context);
});
app.use(logger())//查看加载的详细信息
app.use( staticServe(config.staticPath) );//找到设置的静态文件
app.use( router.routes() );

app.listen(config.port);

console.log( config.port );

你可能感兴趣的:(nodejs 分页)