question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello! tl;dr

how do I create a global middleware for nest that applies nuxt middleware

hey guys I’ve been working on nuxt integration and you can see in this repo how’s the stuff going, however I’m facing a little bit of an issue, I can call Nuxt middleware programatically and add it as an express middleware however this is causing it to catch all routes including the api ones, in order to stop that I need to load nuxt middleware basically at the end of the middlewares, how could I accomplish this for the whole application?

the way you add it to express is like this

import Nuxt from 'nuxt'
import express from 'express'

import api from './api'

const app = express()
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 3000

app.set('port', port)

// Import API Routes
app.use('/api', api)

// Start nuxt.js
async function start() {
  // Import and Set Nuxt.js options
  let config = require('../nuxt.config.js')
  config.dev = !(process.env.NODE_ENV === 'production')
  // Instanciate nuxt.js
  const nuxt = new Nuxt(config)
  // Add nuxt.js middleware
  app.use(nuxt.render)
  // Listen the server
  app.listen(port, host)
  console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console
}

start()

and I i’m using it pretty much like it

import { NestFactory } from '@nestjs/core';
import * as Express from 'express';
import { ApplicationModule } from './modules/app.module';

// tslint:disable-next-line:no-var-requires
const Nuxt = require('nuxt'); // for some reason typings don't pick it up fine as a default import

async function start() {
  const instance = new Express();
  // Import and Set Nuxt.js options
  const config = require('../nuxt.config');
  config.dev = !(instance.env === 'production');
  // Instanciate nuxt.js
  const nuxt = await new Nuxt(config);
  instance.use(nuxt.render);

  const app = NestFactory.create(ApplicationModule, instance);
  app.listen(3000, () => console.log('Application is listening on port 3000.'));
}

start();

and Like I say It does work but intercepts even the routes prefixed with /api like /api/users

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
pirmaxcommented, Jul 9, 2018

@ol-funky Do you have resolve your problem ? I’ve the same problem 😕

https://github.com/pirmax/nuxt-and-nest

1reaction
hawkendcommented, Aug 21, 2017

@AngelMunoz

I think i found solution for your problem. You should pass nuxt middleware to express instance after making nest to run. I’ve checked it and seems to routes of nest are working properly.

server.ts

import { NestFactory } from '@nestjs/core';
import * as Express from 'express';
import { ApplicationModule } from './modules/app.module';

// tslint:disable-next-line:no-var-requires
const Nuxt = require('nuxt');

async function start() {
  const instance = new Express();
  const config = require('../nuxt.config');
  config.dev = !(instance.env === 'production');
  const nuxt = await new Nuxt(config);

  const app = NestFactory.create(ApplicationModule, instance);
  app.listen(3000, () => console.log('Application is listening on port 3000.'));

  instance.use(nuxt.render);
}

start();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt - The Intuitive Vue Framework
Nuxt has a unique approach of combining a great developer experience with reusable, fully integrated features that speed up the development and performance ......
Read more >
Nuxt.js integration - New Relic Documentation
With our Nuxt.js dashboard, you can easily track your application error rate, initial page load average, route changes, throughput, and page views with ......
Read more >
Integrate Contentful with Nuxt.js
This guide will show you how to integrate Contentful in a Nuxt.js application | Setting up the content model using the Contentful CLI....
Read more >
Nuxt: The Intuitive Web Framework
Nuxt is made with a robust plugin system. Integrate with popular CMS or UI libraries with one line of code. With more than...
Read more >
Nuxt.js Integration Guide - Documentation - LexasCMS
Nuxt.js integration guide cover. This tutorial will guide you through the steps required to pull content from LexasCMS into your Nuxt.js project.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found