首页 > 解决方案 > 无法启动用 hapi.js 编写的基本服务器

问题描述

今天,我决定使用 node.js 框架之一,hapi.js。我按照他们的入门指南,安装hapi并放置了启动基本服务器所需的代码,index.js并将其设置npm startnode index.js. 完成所有这些操作后,当我尝试使用 运行服务器时npm start,我遇到了以下错误..:-(。非常感谢任何帮助克服这个问题。

F:\app-Backend\node_modules\@hapi\hapi\lib\core.js:51
   actives = new WeakMap();                                                   // Active requests being processed
            ^
SyntaxError: Unexpected token =
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app-backend@1.0.0 start: `node ./index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the app-backend@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Pavindu\AppData\Roaming\npm-cache\_logs\2020-04-13T08_45_49_367Z-debug.log

index.js

const Hapi = require('@hapi/hapi');

const init = async () => {

    const server = Hapi.server({
        port: 8080,
        host: 'localhost'
    });

    await server.start();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

标签: node.jshapijs

解决方案


该代码对我有用。

你记得npm install @hapi/hapi --save吗?

所以这是因为最新版本的 hapi 只能在节点 12+ 上运行


推荐阅读