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.

【Nuxt】Does Foal support Nuxt middleware?

See original GitHub issue

I try integrating Nuxt with Foal, using Foal starter like

import 'source-map-support/register'

// std
import * as http from 'http'

// 3p
import { Config, createApp } from '@foal/core'
import { connect } from 'mongoose'

// App
import { AppController } from './app/app.controller'
import express from 'express'
import Consola from 'consola'
import { Nuxt, Builder } from 'nuxt'
import config from '../nuxt.config'

const app = express()

// Import and Set Nuxt.js options
config.dev = process.env.NODE_ENV !== 'production'

async function main() {
  // 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()
  }

  // Connect DB
  const uri = Config.get<string>('mongodb.uri')
  connect(uri, { useNewUrlParser: true, useCreateIndex: true })

  let server = createApp(AppController, app)
  server = http.createServer(server)

  // Give nuxt middleware to express
  app.use(nuxt.render)

  const serverPort = Config.get('port', port)
  server.listen(serverPort, host, () => {
    Consola.ready({
      message: `Server listening on http://${host}:${port}`,
      badge: true
    })
  })
}

main()

But it results in 404 for the index route. It seems the controller close the stream if no method is matched. Only /api is available. Are any suggestion or demo ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
LoicPoullaincommented, Aug 8, 2019

Hi @Fnxxxxo

Sorry for the late reply. This is not working because createApp takes care of returning 404 and 500 errors before app.use(nuxt.render) gets called.

It seems the controller close the stream if no method is matched.

Correct.

Currently, there is no quick solution to make it work. Nuxt.js seems a great framework, as well as Next, and we should definitively have an easy way to integrate them with Foal.

I’ll think how to have a clean and straightforward solution to use them in the next few weeks. We should probably have something for this in August or September releases. 👍

1reaction
LoicPoullaincommented, Sep 11, 2019

Feature added in v1.1.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Middleware directory - Nuxt
The middleware directory contains your application middleware. Middleware lets you define custom functions that can be run before rendering either a page or ......
Read more >
Understanding Nuxt Middleware - Debbie O'Brien
Middleware lets you define custom functions that can be run before rendering either a page or a group of pages which we call...
Read more >
Is it possible for middleware to be executed only on client side
When you execute nuxt command in spa mode, middleware will be executed only on the client side. nuxt.js/examples/spa/nuxt.config.js.
Read more >
17 Nuxt JS beginner tutorial - Nuxt middleware - YouTube
Middleware runs before the page loads, so you can do authentication in nuxt using middlewaresNuxtjs Beginner Course ...
Read more >
How to work with Server Middleware in Nuxt.js - YouTube
When you are know more about Middleware in Nuxt.js you will discovered you sometimes need server middleware in Nuxt.js, in this Video I...
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