GithubHelp home page GithubHelp logo

render-server's Introduction

简介

render-server 是一个用于前后端分离的工具。和后端开发者约定好使用的模板引擎(必须支持 Node),通过配置.config文件和自定义的json路由配置文件来渲染获取数据以及渲染模板!

安装

目前还没有放到 npm 上,先通过 git 克隆本项目到本地。�

git clone https://github.com/q545244819/render-server.git
cd render-server
npm link

注意:运行完上面的命令后,不要删除项目,因为全局命令还是调用这个项目的代码!!!

使用

确保你已经安装了 render-server 后,可以再终端使用rs help来查看命令,直接启动渲染服务就使用rs start:

cd my-project
rs start

配置文件

为了能够正确的渲染你写好的模板和路由配置文件,你需要再项目根目录下新建两个文件.config和一个路由配置文件(后缀为json):

.config:

{
  "views": "./views",
  "static": "./public",
  "json": "./route.json",
  "locals": "./locals.json",
  "templateEngine": "ejs",
  "port": 8080
}
属性名 描述 必须
views 模板文件存放的路径(相对于根目录)
static 静态资源文件存放的路径(相对于根目录)
json 路由配置文件存放的路径(相对于根目录)
locals 全局变量配置文件存放的路径(相对于根目录) �否
templateEngine 指定使用模板引擎,目前支持 ejs、jade 和 mustache !
port 访问测试服务器端口

路由配置文件(名称随意),后缀为json

{
  "/": {
    "method": "get",
    "template": "index",
    "data": {
      "title": "Hey",
      "message": "Hello there!"
    }
  },
  "/api": [
    {
      "method": "get",
      "path": "/:name/age",
      "data": {
        "name": "{{ params.name }}",
        "age": 20  
      }
    }
  ]
}

参照上面的路由配置文件 demo,为一个对象,key为路径,value为对象或者数组。

属性名 描述
template 要渲染模板的路径如果没有就返回 JSON 数据
data 传入的数据
path 如果是根目录下面子目录,则这个为子目录的路径

注意:如果value是数组的话,你需要在每一个数组元素(对象)设置一个path为子路径,如上面的/api的写法,你可以访问/api/:name/age获取对应的数据。

全局变量配置文件

有可能某些数据是在所有页面都会使用到的,例如用户的信息等,所以我们需要单独配置一个全局变量配置文件,我一般命名为locals.json

例子:

{
  "user": {
    "name": "john",
    "mobilePhoneNumber": "110"
  }
}

全局变量配置文件就是一个普通的json文件,然后只需要把用到的假数据写进去。你就可以直接在模板里面使用到了,不需要每一个重新定义一次。不过要注意自己之前路由配置文件里面data是有和全局变量重名的属性!

获取 query、params 和 body 的值

如果你想要获取 query string、 params 和 post body 的值的话,并且返回来。你可以再data里使用模板,如:

注意:只能再路由配置文件的data里面使用!!!

{
  "/": {
    "method": "get",
    "template": "index",
    "data": {
      "message": "{{ query.message }}"
    }
  }
}

然后访问/?message=hello的话,data里的message值会通过模板编译为对应的值。

  • query:获取 query string 的值,使用方法:{{ query.demo }}
  • params:获取 params 的值,使用方法:{{ params.demo }}
  • body:获取 post body 的值,使用方法:{{ body.demo }}

TODO

  • 支持全局变量
  • 修改配置文件自动重新启动项目
  • 支持 handlebars 模板语言

Licences

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.