Denoder / nuxt-module-alternatives

Alternative modules to use while waiting for Nuxt 3 Compatibility
MIT License
144 stars 14 forks source link

[Proxy] "http proxy error:Error: socket hang up" on Windows #134

Closed lackneets closed 1 year ago

lackneets commented 1 year ago

Environment

Nuxi 3.0.0                                                                                                                                                                                 

RootDir: C:/Users/User/works/Web/frontend                                                                                                                                         
Nuxt project info:                                                                                                                                                                          

------------------------------
- Operating System: `Windows_NT`
- Node Version:     `v18.12.1`
- Nuxt Version:     `3.0.0`
- Nitro Version:    `1.0.0`
- Package Manager:  `yarn@1.22.19`
- Builder:          `vite`
- User Config:      `modules`, `ssr`, `proxy`, `css`, `vite`
- Runtime Modules:  `@nuxtjs-alt/proxy@2.1.0`, `()`
- Build Modules:    `-`
------------------------------

Nuxt Config

import vuetify from 'vite-plugin-vuetify'

export default defineNuxtConfig({
  modules: [
    '@nuxtjs-alt/proxy',
  ],
  ssr: true,
  proxy: {
    enableProxy: true,
    proxies: {
      '^/(api)/?.*': {
        target: `http://localhost:${process.env.RAILS_PORT || 3001}`,
        changeOrigin: true,
        ws: false
      },
    },
    fetch: true
  },
})

Reproduction

(It's a bug related to platform)

Describe the bug

I run Nuxt at port 3000 while there is a rails api server at 3001. In this case, I setup http://localhost:3000/api/* proxies to http://localhost:3001/api/*.

This is both works on a Alpine OS and my MacOS Ventura. However it breaks on my colleague's Windows 10 also mine when api get fetched by SSR. But I can normally access to http://localhost:3000/api/* without any error. I've tried other public domain URL, but nothing changes.

Furthermore, when ssr set to false will prevent this error.

For this reason, I'm convinced that is a bug highly related to Window Platform and SSR. Maybe it's caused by Vite or Nitro.

Additional context

I've checked bellow links, but nothing helps:

Logs

http proxy error:Error: socket hang up                                                                                                                                                      17:12:17  
    at connResetException (node:internal/errors:711:14)
    at Socket.socketCloseListener (node:_http_client:467:25)
    at Socket.emit (node:events:525:35)
    at TCP.<anonymous> (node:net:313:12) {
  timestamp: true,
  error: Error: socket hang up
      at connResetException (node:internal/errors:711:14)
      at Socket.socketCloseListener (node:_http_client:467:25)
      at Socket.emit (node:events:525:35)
      at TCP.<anonymous> (node:net:313:12) {
    code: 'ECONNRESET'
  }
}
/api/funds -> http://localhost:3001                                                                                                                                                         17:12:36
http proxy error:Error: socket hang up                                                                                                                                                      17:12:36
    at connResetException (node:internal/errors:711:14)
    at Socket.socketCloseListener (node:_http_client:467:25)
    at Socket.emit (node:events:525:35)
    at TCP.<anonymous> (node:net:313:12) {
  timestamp: true,
  error: Error: socket hang up
      at connResetException (node:internal/errors:711:14)
      at Socket.socketCloseListener (node:_http_client:467:25)
      at Socket.emit (node:events:525:35)
      at TCP.<anonymous> (node:net:313:12) {
    code: 'ECONNRESET'
  }
}
Denoder commented 1 year ago

probably has to do with this issue https://github.com/nuxt/nuxt.js/issues/15492. I had specifically disabled nitro options on windows platforms with the modules because of pathing issues. maybe it's fixed now, i remove d the restrictions on the module so you can test it out, bout other than that I wouldn't have a fix.

lackneets commented 1 year ago

Thanks for the information. Following your clue. I found the problem is caused by Nitro:resolvePath

Workaround

The windows formatted path is got transformed incorrectly by Nitro. I think it's nothing to do with Nuxt itself.

The solution is to manually hack this file by adding file:// before ${plugin}, like:

${nitro.options.plugins.map(plugin => `import _${hash(plugin)} from 'file://${plugin}';`).join('\n')}

Tested on linux-like OS. It' correct to add file:// on any of absolute path import.

I'll report to Nitro later.

TLDR

Nitro will generate a script at .nuxt/dev/index.mjs before server up:

//...
import { toRouteMatcher, createRouter } from 'file://C:/Users/User/works/Web/frontend/node_modules/radix3/dist/index.mjs';
import _ROuXHMtK6R from 'C:/Users/User/works/Web/frontend/.nuxt/nitro-fetch.mjs';
import { createProxyServer } from 'file://C:/Users/User/works/Web/frontend/node_modules/@refactorjs/http-proxy/dist/index.mjs';
//...

Then node complains:

ERROR  [worker reload] [worker init] Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

In brief,

C:/foo/bar.js

should be corrected to:

file://C:/foo/bar.js
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.