Closed ghost closed 5 years ago
here's some code to repro:
gatsby-config.js
developMiddleware: app => {
app.use('/.netlify/functions/', (request, response, next) => {
console.log('hit');
next();
});
app.use(
'/.netlify/functions/',
proxy('/.netlify/functions/', {
logLevel: 'debug',
target: 'http://localhost:9000',
pathRewrite: function(path, req) {
console.log('proxying', path);
const newPath = path.replace('/.netlify/functions', '');
console.log('newPath', newPath);
return newPath;
},
// {
// '^/\\.netlify/functions': '',
// },
})
);
},
starting gatsby
$ yarn start
...
[HPM] Proxy created: /.netlify/functions/ -> http://localhost:9000
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
...
ℹ 「wdm」: Compiled successfully.
curling the netlify function (404 HTML returned)
$ curl http://localhost:8000/.netlify/functions/hello
<!DOCTYPE html><html><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"/><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/><title data-react-helmet="true"></title><link rel="manifest" href="/manifest.webmanifest"/><meta name="theme-color" content="#663399"/><script src="/socket.io/socket.io.js"></script></head><body><noscript id="gatsby-noscript">This app works best with JavaScript enabled.</noscript><div id="___gatsby"></div><script src="/commons.js"></script></body></html>%
output from gatsby server
...
hit
proxying /.netlify/functions/hello
newPath /hello
[HPM] GET /.netlify/functions/hello ~> http://localhost:9000
localhost:9000 does not received any requests.
for reference, when i use changeOrigin: true
, the requests hang, and when i don't, they're being proxied to the local dev server (as opposed to localhost:9000
).
hi andy
does this project run if you clone it? https://github.com/sw-yx/jamstack-hackathon-starter
just trying to narrow down what the cause of this is
Hey @andycunn
I just cloned the repository, added Gatsby, the gatsby-config.js
from https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying and moved App into pages so that Gatsby creates a page for it and it seems to work fine.
Here's my fork: https://github.com/sidharthachatterjee/create-react-app-lambda
And here is the diff: https://github.com/netlify/create-react-app-lambda/compare/master...sidharthachatterjee:master
Could you please try this and let us know if it works?
Here's a gif
Ran gatsby develop
and npm run build:lambda
and then npm run start:lambda
right. my hunch is andy got some small part of the config wrong. need to check closely.
trying your suggestions. will check back in a bit
hmmm maybe this is environmental? can't get https://github.com/sw-yx/jamstack-hackathon-starter to work.
yarn
yarn start
Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0
response is the empty gatsby index
<!DOCTYPE html><html><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"/><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/><title data-react-helmet="true"></title><link rel="shortcut icon" href="/icons/icon-48x48.png"/><link rel="manifest" href="/manifest.webmanifest"/><meta name="theme-color" content="#663399"/><script src="/socket.io/socket.io.js"></script></head><body><noscript id="gatsby-noscript">This app works best with JavaScript enabled.</noscript><div id="___gatsby"></div><script src="/commons.js"></script></body></html>
any environmental info i can provide that would be helpful?
FYI @sidharthachatterjee i had to use SKIP_PREFLIGHT_CHECK=true
to yarn your fork, dunno if that's helpful info.
also your fork did not compile when i cloned it: https://github.com/sidharthachatterjee/create-react-app-lambda
Failed to compile.
./src/index.js
Module not found: Can't resolve './App' in '/Workspace/sandbox/jamstack-hackathon-starter/function-bug-tester/src'
this seems an environment issue then. something is up in your computer that is getting in the way of your ability to do a proxy. read the error messages closely - anything jump out?
there aren't really any error messages. any way i can turn on some more low-level logging there? We do use ZScaler network security in my office. wonder if that could interfere?
that was it! got our IT to turn off our traffic monitor and it's all good now :)
sorry for the confusion. closing this out.
loll that was rough. glad u figured it out
thanks for your help and your time!
Description
Trying to use the advanced proxying example as-is for netlify functions.
The proxy doesn't seem to work, as i get 404 when i try to call
/.netlify/functions/hello
.I see the same repro when i use the vanilla create-react-app-lambda repository.
Calls to the
.netlify
endpoint fail to proxy.Steps to reproduce
yarn start
call function
link.Expected result
http://localhost:3000/.netlify/functions/hello
should proxy to the hello lambda and return json. (when usinggatsby develop
, GET to ':8000` for same repro)Actual result
GET to above url returns home dev server index. (when using
gatsby develop
, GET request returns 404)Environment