我开始使用Hapi nodejs框架.我使用的是"hapi@17.2.0",这是我在server.js中的代码来启动应用程序.
'use strict';
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 3000, host: 'localhost' });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('Hello, world!');
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});
Run Code Online (Sandbox Code Playgroud)
node server.js从终端运行我的项目之后,它会抛出错误,如下所示.
/var/www/html/hello_hapi/server.js:6
server.connection({ port: 3000, host: 'localhost' });
^
TypeError: server.connection is not a function
at Object.<anonymous> (/var/www/html/hello_hapi/server.js:6:8)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at …Run Code Online (Sandbox Code Playgroud) 为了进行负载测试,我创建了一个 API,它一次请求大约 2000 个预订,在点击这个 API 后,服务器(AWS EC2 实例)达到了超过 20% 的 CPU 消耗并消耗了几乎所有的内存(900/991M ) 的系统。在那之后,我试图用 pm2 restart 重新启动节点应用程序,但它不起作用,甚至“pm2 ls”和“pm2 log”命令也不起作用。我现在该怎么做才能再次运行我的应用程序?我已经在服务器上运行的命令列表。
pm2 restart pid && pm2 log pid
pm2 ls
pm2 log pid
Run Code Online (Sandbox Code Playgroud) 我开始使用 hapijs 并用它设置了一个新的练习项目以进行更深入的研究。我成功设置了一次,它工作正常,但在运行 node server.js 时突然开始抛出错误,如下所示。
/Library/WebServer/Documents/hello_hapi/node_modules/hapi/lib/server.js:107
decorate(type, property, method, options = {}) {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/Library/WebServer/Documents/hello_hapi/node_modules/hapi/lib/index.js:5:16)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
Run Code Online (Sandbox Code Playgroud) 我正在尝试从官方节点 api 文档中运行 Async Hooks 的测试代码,但Error: Cannot find module 'async_hooks'在控制台中出现错误。我已经包含const async_hooks = require('async_hooks');在我的脚本的顶部。这是我的代码:
const async_hooks = require('async_hooks');
// Return the ID of the current execution context.
const eid = async_hooks.executionAsyncId();
// Return the ID of the handle responsible for triggering the callback of the
// current execution scope to call.
const tid = async_hooks.triggerAsyncId();
// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve }); …Run Code Online (Sandbox Code Playgroud) node.js ×4
hapijs ×2
amazon-ec2 ×1
async-hooks ×1
asynchronous ×1
module ×1
pm2 ×1
server ×1
ubuntu-16.04 ×1
version ×1