问题描述
我正在尝试将 newrelic 代理添加到我的 nuxt 应用程序中.我已经安装了所需的包并添加了我的许可证密钥并在 newrelic.js
配置文件中设置了一个应用程序名称:
I am trying to add newrelic agent to my nuxt application. I have installed the needed package and added my license key and set an application name in newrelic.js
configuration file:
npm i newrelic
cp node_modules/newrelic/newrelic.js .
nano newrelic.js
我的问题是我还需要在我的 server.js
文件的顶部需要这个配置文件,因为这个文件是动态创建的并放置在 .nuxt
文件夹我不知道该怎么做.
My problem is that I also need to require this configuration file at the top of my server.js
file and since this file is dynamically created and placed under the .nuxt
folder I have no idea how to do this.
在标准的 nodejs 应用程序中,我只需将 require('newrelic');
添加到我的启动脚本的顶部,或者在 package.json
中添加一个新的脚本条目代码>看起来像这样:
In a standard nodejs application I would simply add the require('newrelic');
to the top of my startup script or perhaps add a new script entry in package.json
looking something like this:
"scripts": {
"dev": "node -r newrelic.js app.js"
}
推荐答案
我最终使用 express
来解决这个问题:
I ended up using express
to solve this:
npm i express
touch server/index.js
我们现在将在 server/index.js
文件中加载 newrelic
,然后创建我们的 nuxt 实例:
We will now load newrelic
in the server/index.js
file and after that create our nuxt instance:
require('newrelic');
const express = require('express');
const consola = require('consola');
const { Nuxt, Builder } = require('nuxt');
const app = express();
// Import and Set Nuxt.js options
const config = require('../nuxt.config.js');
config.dev = process.env.NODE_ENV !== 'production';
async function start () {
// Init Nuxt.js
const nuxt = new Nuxt(config);
const { host, port } = nuxt.options.server;
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt);
await builder.build();
} else {
await nuxt.ready();
}
// Give nuxt middleware to express
app.use(nuxt.render);
// Listen the server
app.listen(port, host);
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
});
}
start();
我还更新了 package.json
中的 script
部分:
I also updated the script
section in my package.json
:
"scripts": {
"dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",
"build": "nuxt build",
"start": "cross-env NODE_ENV=production node server/index.js"
}
希望这可以帮助任何面临同样问题的人.
Hope this can help anyone who faces the same kind of problem.
这篇关于在nuxt中使用newrelic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!