首页 > 解决方案 > 在 Hapi 我得到 h.file is not a function

问题描述

我正在运行最新版本的 hapi,但是当我尝试服务器静态文件时,我收到错误 h.file is not a function。我已经安装了惰性。这是相关代码和我的 package.json

包.json

{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "import_data.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ag-grid-angular": "^19.1.2",
    "ag-grid-community": "^19.1.4",
    "hapi": "^17.8.1",
    "inert": "^5.1.2",
    "npm": "^6.4.1",
    "sqlite3": "^4.0.4"
  }
}

路线代码

// Create a server with a host and port
const server=Hapi.server({
    host:'localhost',
    port:8000,
    routes: {
        files: {
            relativeTo: Path.join(__dirname, 'mibfiles')
        }
    }
});

server.route({
    method: 'GET',
    path: '/mibfiles/{mibname}',
    handler: function(request, h) {
        return h.file('mibfiles/' + request.params.mibname + ".mib")
    }
})

标签: node.jshapi

解决方案


忘记在 hapi 中注册惰性对象 await server.register(require('inert'));


推荐阅读