2

I have a project built with Nuxt.js and I want to use express to be able to report bugsnag errors on my asyncData method etc.

How would I go to import that? I suppose is not as simple as npm install express --save.

I already have an api written in PHP so I would not use that as an api or anything else.

Is it overkill? Or is it a necessary evil? :D

2 Answers 2

8

To start using Express with an existing Nuxt project, you'll need to set up a simple server.js file that sets up your Express server and adds your Nuxt application as middleware. The only slight complication is setting it up to auto-rebuild in development. Here's a quick example server.js file that pulls in Nuxt and handles building when not in production.

const { Nuxt, Builder } = require('nuxt');
const app = require('express')();


// We instantiate Nuxt.js with the options
const isProd = process.env.NODE_ENV === 'production';
const config = require('./nuxt.config.js');

config.dev = !(isProd);
const nuxt = new Nuxt(config);

// No build in production
if (!isProd) {
  const builder = new Builder(nuxt);
  builder.build();
}
app.use(nuxt.render);
app.listen(3000);
console.log('Server is listening on http://localhost:3000');

You can then incorporate bugsnag as you normally would in express, I assume by requiring it and including it as middleware.

Sign up to request clarification or add additional context in comments.

1 Comment

Why would you only want to build in dev env? If you set config.dev to false Builder will start production build. If I don't build my app first it can't be served or am I wrong?
0

You dont need express to handle errors in asyncData. To handle errors in asyncData/fetch on ssr you just need to hook into render:errorMiddleware. See sentry plugin for nuxt for example

But it would only catch errors that happens on SSR. On client unfortunately that wont catch anything ( as well as express wont do anything for client). There is a bug for this in nuxt see here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.