2017-02-12 33 views
0

我想用seneca-web(express)創建其他API。我找不到任何(完整)文檔中使用的路由文件。我基地一個these examples。假設我有一個名爲Task的資源。我想有這些HTTP方法:如何在seneca-web中使用路線作爲休息api?

  • GET /任務
  • GET /任務/:任務id
  • POST /任務

這裏是routes.js:

module.exports = [ 
    { 
    prefix: '/tasks', 
    pin: 'role:api,path:*', 
    map: { 
     all: { 
     GET: true, 
     prefix: '' 
     }, 
     ':taskId': { 
     GET: true 
     } 

    } 
    }, 
    { 
    pin: 'role:api,path:*', 
    map: { 
     tasks: { 
     POST: true 
     } 

    } 
    } 
] 

和我的seneca插件進行處理:

module.exports = function task (options) { 
    this.add({role: 'api', path: 'all'}, function (msg, respond) { 
    console.log(msg) 
    this.act('role:task,cmd:all', respond) 
    respond(null, [{name: 'First Task', description: 'Description of  the First Task'}]) 
    }) 
    this.add({role: 'api', path: '*'}, function (msg, respond) { 
    console.log(msg) 
    this.act('role:task,cmd:single', {taskId: msg.args.params.taskId}, respond) 
    }) 
} 
  • 我不知道如何在這裏分開POST和GET操作。
  • 我發現還有一個問題,即路徑的地圖對象中的鍵被視爲路徑的一部分,例如。 GET/tasks/all而不是GET /任務。

感謝您的幫助。

+0

你們看透了這一點? –

+0

@aman_novice是的,我在那裏使用了別名,但我不喜歡Seneca與express.js的集成:) – hexin

+0

我希望你使用Seneca來開發一個使用微服務架構的應用程序。你能建議你選擇的替代路線嗎? –

回答

-1

這裏是示例塞內卡的web使用命令路由

========= index.js =======

const seneca = require('seneca')() 
const express = require('express')() 
const web = require('seneca-web') 
const cors = require('cors') 

var Routes = [{ 
    prefix: '/products', 
    pin: 'area:product,action:*', 
    map: {list: {GET: true}} 
}] 
express.use(cors()) 
var config = { 
    routes: Routes, 
    adapter: require('seneca-web-adapter-express'), 
    context: express, 
    options: {parseBody: true} 
} 
seneca.client() 
.use(web, config) 
.ready(() => { 
    var server = seneca.export('web/context')() 
    server.listen('8082',() => { 
    console.log('server started on: 8082') 
    }) 
}) 
seneca.add({area: 'product', action: 'list'}, function (args, done) { 
    try { 
    done(null, {response: 'Product List'}) 
    } catch (err) { 
    done(err, null) 
    } 
}) 

啓動應用的: 節點index.js

在瀏覽器中打開

鏈接 http://localhost:8082/products/list

+0

這不是問題中提出的問題。我知道如何基本配置塞內卡。 – hexin