内容平台

Setup

# global install

npm install @ali/hapi -g

hapi -p 5000

# or manually install (recommend)

git clone @ali/hapi.git hapi-server

cd hapi-server && yarn --prod # install dependencies

vi config.json # set your config

npm start # run server

Config

{
  "test": {
    "server": {
      "hostname": "",
      "port": ""
    },

    // external MongoDB server
    "db": {
      "host": "",
      "port": "",
      "username": "",
      "password": ""
    }
  },

  "production": {
    // same as above
  }
}

config.json

interface Content {
  title: string,
  content?: string,
  created_at: Date,
  updated_at: Date,
  source: string,
  label: ObjectId[],
  image: [
    {
      // ...
    }
  ],
  video: [
    {
      // ...
    }
  ],
  custom_info: {
    // ...
  }
}

Content Object

SDK

import Hapi from '@ali/node-hapi-sdk'

Hapi.config({
  host: '123.456.789.100',
  port: '5000'
})
const news = new Hapi.Content

// add image
news.addImage({
  url: 'foo.png',
  width: /** ... */,
  height: /** ... */
})

// add video
news.addVideo({
  url: 'foo.mp4',
  duration: /** ... */
})

// set content
news.setContent('<div>foo</div>')

// set source
news.from('scrapy')

news.save()
  .then(newsObj => {/** ... */})
  .catch(e => {/** ... */})

Save a new content

Content Object

WebHook

POST

WebHook

Do whatever

// Hook controller for handling content object
module.exports = function * () {
  const contentObject = this.body

  // transform content object with your own interface
  newContentObject = transform(contentObject)

  yield db.save(newContentObject)
}

HAPI

By Randy Lu