更新:添加了完整的服务器代码。请注意,静态内容的路由工作正常,只有相对于Dat的路由失败。另外,我正在运行没有10.8.0
的节点micro -l tcp://0.0.0.0:$PORT
,服务器运行了dat-node
我正在尝试使用Zeit micro
运行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)
)
。我有这个微服务
send
我只想创建一个dat存档并返回公钥,但是当我致电(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.
时,我会收到此错误
{{1}}
因此,我不确定标头发送到的位置,也许我只是以错误的方式处理Dat回调?不确定如何实施此服务。
答案 0 :(得分:2)
问题似乎与micro
的使用有关,与dat-node
无关。在微型计算机中进行异步工作时,您需要使用Javascript(承诺)的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 })
}
答案 1 :(得分:1)
如果您导出该功能,则该代码段将起作用。用micro whatever.js
运行,然后卷曲到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') })
})
}