Functions, lambdas, serverless. Maybe you've heard of these. Maybe you haven't. These words are being used to describe a different way of connectin...
For further actions, you may consider blocking this person and/or reporting abuse
Hello Jenna,
I got stuck at the first step :(
when I do : "netlify functions:invoke send-contact-email --no-identity"
I get the following error :
$ netlify functions:invoke send-contact-email --no-identity
ran into an error invoking your function
{ FetchError: request to http://localhost:8888/.netlify/functions/send-contact-email failed, reason: connect ECONNREFUSED 127.0.0.1:8888
at ClientRequest.<anonymous> (/usr/local/lib/node_modules/netlify-cli/node_modules/node-fetch/lib/index.js:1455:11)
at ClientRequest.emit (events.js:198:13)
at Socket.socketErrorListener (_http_client.js:392:9)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
message:
'request to http://localhost:8888/.netlify/functions/send-contact-email failed, reason: connect ECONNREFUSED 127.0.0.1:8888',
type: 'system',
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED' }
Any tips on how to get ride of this error?
Hi Marie -
The
ECONNREFUSED
error indicates that it can't connect to where the function is hosted (in this case localhost:8888). Make sure that you havenetlify dev
running in another command line window so that the functions are accessible.Hope that helps!
When I run netlify dev I get
and when running
netlify functions:invoke send-contact-email --no-identity
Did you resolve this? I'm getting exactly the same issue trying to call a simple function using netlify dev. Have spent ages on it.
I used 'netlify.toml' to define functions="functions/"
Is this possibly a WIndows issue?
Hi Steve - Did you get this ironed out? What's the error message you're seeing?
If it's the same as the one that Marie ran into
reason: connect ECONNREFUSED 127.0.0.1:8888
then make sure that you've started up your app with
netlify dev
first.If it is started, check to see what port is being used. You should see a message that the server has started. There are 3 that are started - the app, the lambda server where your functions run, and a proxy server (which is a proxy to both of those), so be sure to look for the one that is for the proxy server. The message looks like this:
If that port is NOT 8888, then the
netlify functions:invoke
call will fail since it's looking for functions on port 8888.It doesn't look like there is an option in the command line to specify a port here, so there are a couple of options: 1) Figure out what is using port 8888 and stop it so that this will work or 2) Add the following to your
[dev]
block in yournetlify.toml
file:This will force it to use the port you tell it to use and
netlify functions:invoke
will also use that port.Make sure to change 4444 to an unused port on your system.
Actually, it seems the lastest netlify-cli is broken. After a lot of head scratching I found this issue and intalled an old version and the problem went away
github.com/netlify/cli/issues/659
Thanks for answering so quickly
Oh bummer! Glad you got it sorted though.
Thanks for your quick reply. I had the netlify dev running in another command line, that's why I don't understand ^
My error is: Function invocation failed: [object Object] - without the payload and with it... ummm
agh, yes - install the dependencies, well done genius - now I'm forbidden, but definitely getting there!!
I'm loving the tutorial - it is super helpful, Jenna, thanks
Thanks! Glad you got it ironed out.
Sadly not yet - I'm now forbidden - I can connect and send using curl but the js version is rejected.
Here is the function paired down to basics with hard-coded everything just to get mailgun working
const mailgun = require('mailgun-js');
exports.handler = async (event) =>
{
const mg = mailgun({
apiKey: "key-exampleofverylongsecretkey",
domain: "mg.example.com",
url: "api.eu.mailgun.net/v3/mg.example.com"
});
const data = {
from: 'Name mailgun@mg.example.com',
to: 'bored@otherexample.com',
subject: 'Worldish',
text: 'Hello W',
html: 'HTML'
};
return mg.messages().send(data).then(() => ({
statusCode: 200,
body: "Your message was sent successfully! We'll be in touch."
})).catch(error => ({
statusCode: 422,
body:
Error: ${error}
}));
}
I went back to testing with the sandbox and the email sent - but ended up in spam. -which could be reasonable since it claims to be from one domain and has been sent by another. - I guess I just have to test it on the server
If I hardcode sandbox secrets into the file it sends happily from the localhost. If I then switch to using dotenv it fails suggesting that require('dotenv').config() isn't loading up process.env
Reinstalling dotenv has sorted the problem
Great post!
I've loved playing around with Netlify functions -- it makes working with AWS lambda so much more approachable (for me, at least.) And it's neat to see how quickly it lets you build out small apps with backend functionality (like email!) without having to worry about the operations of standing up a backend server.
But, on the flip side, have you run into any difficulties with them? I've found debugging to be a bit more difficult and time-consuming, but that might subside as I become more familiar with them and as netlify dev gets a bit more mature.
Thanks for the feedback, Tim!
Such a great question too. I have one live project (other than this playground) using a few functions and with not much traffic, so I haven't had a lot of experience with where it falls down. One thing I found (for that particular implementation) was that since there was not much traffic hitting those functions, they would occasionally fail/timeout as they were spinning up. Another use case I've heard they are not well suited for is long-running processes because of this low timeout value.
I started digging into this because I wanted to learn more about that specifically so that I can make better decisions on whether to go down this route for future projects. Stay tuned for a future post on!
And I'm all ears on where you find it tricky or where it doesn't work well, beyond the debugging process.
netlify functions:invoke send-contact-email --no-identity --payload '{"contactEmail" : "jenna@example.com", "contactName" : "Jenna", "message" : "hello world from a function!"}'
this produces this error:
Error: Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings.
Hey Jeff -
Based on that error, you'll either have to add the email address you're using in the
CONTACT_TO_EMAIL_ADDRESS
env var to the authorized recipients in your Mailgun Account Settings (see more about that here) or move off the sandbox domain to your very own custom domain.Hope that helps!
Is yarn really necessary in 2019?
I seem to be getting away with using npm - but I'm coming late to the party :-)