/post 包含使用 request.payload (hapi.js) 的数据的请求

/post request containing data using request.payload (hapi.js)

我正在尝试使用 Hapi.js.

向我的服务器发出一个提供数据(保存到数据库(knex))的 /post 请求

连同 post 请求,我正在尝试使用 2 个函数将随机数转换为十六进制字符串并将其设置为我的数据对象中的 hexString 键。

routes.js

module.exports.postData = {
  method: 'POST',
  path: '/',
  handler: (request, reply) => {
    const post = request.payload
    const data = {
      id: uuid.v4(),
      timeOut: null,
      uri: post.uri,
      payload: post.payload,
      hexString: undefined
    }
    store.link.createRandomInt()
      .then((randomNum) => {
        store.link.createHexString(randomNum)
          .then((hex) => {
            data.hexString = hex
            reply(store.link.createLink(data)).code(201)
          })
      })
   }
}

functions.js

module.exports.createRandomInt = function () {
  return new Promise(function (resolve, reject) {
    var randomNumber = Math.floor((Math.random() * 100000000))
    resolve(randomNumber)
  })
}

module.exports.createHexString = function (int) {
  return new Promise(function (resolve, reject) {
    var hexString = int.toString(32)
    resolve(hexString)
  })
}

dbFunctions.js

module.exports = {
  createLink: function (link) {
    return db('links').insert(link)
      .then((id) => {
        return {
          id: uuid.v4(),
          timeOut: null,
          uri: link.uri,
          payload: link.payload,
          hexString: link.hexString
        }
      })
   }
}

Post 卷曲命令:

curl http://localhost:8000/ -d "uri: 'this is the uri' payload: { jsonObj: 'enter payload info here'}"

什么Post卷曲命令Returns:

{"id":"87c467dd-3703-4f9f-a1ac-78551dc33109","timeOut":null}

当我console.log()返回的数据对象:

Promise {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }

感谢任何输入~

我发现它为什么不发布数据了。 我的 curl 命令错误。

正确的 curl 命令如下:

curl -H "Content-Type: application/json" -X POST -d '{"uri": "this uri", "payload": "enter payload info here"}' http://localhost:8000/

routes.js

handler: (request, reply) => {
    const post = request.payload
    const key = store.util.createRandomInt()
    const data = {
      id: uuid.v4(),
      timeOut: null,
      uri: post.uri,
      payload: post.payload,
      key: key,
      hexString: store.util.convertToHexString(key)
    }
    const result = store.sqlStore.createLink(data)
    reply(result).code(201)
}