【问题标题】:How to run dat-node as a micro service?如何将 dat-node 作为微服务运行?
【发布时间】:2019-01-15 22:42:01
【问题描述】:

更新:添加了完整的服务器代码。请注意,静态内容的路由工作正常,只有与 Dat 相关的路由失败。另外,我正在运行节点10.8.0,没有转译器或任何东西,服务器运行micro -l tcp://0.0.0.0:$PORT


我正在尝试使用 Zeit micro 运行 dat-node。我有这个微服务

const { send } = require('micro')
const Dat = require('dat-node')
const handler = require('serve-handler')
const { router, get, post } = require('microrouter')

const static = async (request, response) => {
  await handler(request, response, {
  // static app folder
  public: 'static',
    // javascript header for es modules
    headers: {
      source: '**/*.mjs',
      headers: [{
        key: 'Content-Type',
        value: 'text/javascript'
      }]
    },
    // no directory listing
    directoryListing: false
  })
}

const createGame = (request, response) => {
  Dat('./game', (err, dat) => {
    if (err) throw err

    let progress = dat.importFiles({watch: true})

    progress.on('put', function (src, dest) {
      console.log('Importing ', src.name, ' into archive')
    })

    dat.joinNetwork()

    send(response, 200, { key: dat.key.toString('hex') })
  })
}

const joinGame = (request, response) => {

}

module.exports = router(
  post('/game', createGame),
  post('/game/:game', joinGame),
  get('/*', static)
)

我只想创建一个 dat 存档并返回公钥,但是当我调用 send 时出现此错误

(node:2054) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:469:11)
    at send (/home/ubuntu/workspace/node_modules/micro/lib/index.js:72:8)
    at Dat (/home/ubuntu/workspace/index.js:35:5)
    at /home/ubuntu/workspace/node_modules/dat-node/index.js:112:9
    at apply (/home/ubuntu/workspace/node_modules/thunky/index.js:44:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)(node:2054) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)(node:2054) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

所以,我不确定标头被发送到哪里,也许我只是以错误的方式处理 Dat 回调?不知道如何实施这项服务。

【问题讨论】:

  • 只发送一个空响应(没有密钥)是否有效?或者,如果您在回调中删除 async,则您拥有的代码可能不需要。
  • @joehand 如果我删除 async 关键字,我会得到同样的错误,但是这次服务器进程被杀死,使用 sn -p,记录了异常,但是服务器继续运行

标签: node.js micro dat-protocol


【解决方案1】:

如果你导出函数,sn-p 就可以工作。使用micro whatever.js 运行并 curl 到 localhost:3000,它应该会返回密钥。

const { send } = require('micro')
const Dat = require('dat-node')

module.exports = (request, response) => {
  Dat('./game', async (err, dat) => {
    if (err) throw err

    let progress = dat.importFiles({watch: true})

    progress.on('put', function (src, dest) {
      console.log('Importing ', src.name, ' into archive')
    })

    dat.joinNetwork()

    send(response, 200, { key: dat.key.toString('hex') })
 })
}

【讨论】:

  • 我实际上是在导出。我将使用服务器的整个代码更新我的问题。
【解决方案2】:

问题似乎是micro 的使用,而不是dat-node 的任何问题。当你在 micro 中做异步工作时,你需要使用 Javascript (promises) 的 async/await 工具。

这里是固定代码:

const createGame = async (request, response) => {
  var key = await new Promise((resolve) => {
    Dat('./game', (err, dat) => {
      if (err) throw err

      let progress = dat.importFiles({watch: true})

      progress.on('put', function (src, dest) {
        console.log('Importing ', src.name, ' into archive')
      })

      dat.joinNetwork()
      resolve(dat.key.toString('hex'))
    })
  })
  console.log('sending')
  send(response, 200, { key })
}

【讨论】:

  • 完全正确。我用node promisify函数写了const dat = await Dat('./game'),其实和你写的一样。谢谢:)
猜你喜欢
  • 2017-04-09
  • 2016-08-06
  • 2015-08-13
  • 2012-05-14
  • 2018-02-21
  • 2011-01-27
  • 2019-08-19
  • 1970-01-01
相关资源
最近更新 更多