Code Monkey home page Code Monkey logo

nuxt-payload's Introduction

nuxt-payload

Yet another approach to simplyfying backend data delivery

Simple example

  • in server middleware
const Router = require('router')

const USERS = [
  {id: 1, name: 'foo'},
  {id: 2, name: 'bar'},
  {id: 3, name: 'baz'}
]

const api = new Router()

api.get('/', (req, res, next) => {
  req.payload({users: USERS})
})

api.get('/:id', (req, res, nuxt) => {
  let match = USERS.filter(user => user.id === req.params.id)
  if (match.length === 1) {
    req.payload(match[0])
  } else {
    nuxt(new Error("No such user"))
  }
})
  • in page component
<template>
  <div><nuxt-link :to="{name: 'user-profile', params: {id: user.id}}">Hello, {{user.name}}</nuxt-link></div>
</template>
<script>
export default {
  asyncData({app}) {
    return app.$payload()
  }
}
</script>

The only thing to do is to create a route function for connect that points to the same endpoint as the nuxt route. In SSR, the payload is simply passed via res object without any http overhead. In CSR, $payload method will automatically try to get payload by sending an http request to the same url as the route. The payload module automatically creates an endpoint that responds to the json version of the payload for the Accept request for application/json.

This allows you to easily and quickly merge isomorphic backend code without the need for unnecessary server / client script branching by simply writing a single backend code.

Build Setup

# install dependencies
$ npm install # Or yarn install

# serve with hot reload at localhost:3000
$ npm run dev

# build for production and launch server
$ npm start

nuxt-payload's People

Contributors

comfuture avatar

Watchers

 avatar  avatar

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.