How to disable function logs on Gatsby? - node.js

So, I am new to gatsby and I am trying to build gatsby functions the way it is written in this documentation: https://www.gatsbyjs.com/docs/reference/functions/getting-started/
However, whenever I execute the function, it logs something like "Executed function "/api/test" in 27ms".
Now, since this will be a frequently called function in the future, I wish to disable the logs preferably just for this function OR for all the functions under /api folder.
Can anyone help please me out?

Try using gatsby-plugin-remove-console plugin:
module.exports = {
plugins: [
'gatsby-plugin-remove-console'
],
};
There isn't any built-in method of disabling logs as far as I know so try using the plugin. However, I'm not sure about its use in Gatsby functions...

Related

GCP Cloud Functions not looking for function.js

According to GCP doc
Cloud Functions will look for files with specific names for deployable functions. For Node.js, these filenames are index.js or function.js.
Source: https://cloud.google.com/sdk/gcloud/reference/functions/deploy#--source
In my function.js file, I have:
exports.myFunction = async (req, res) => {}
And I am deploying with this command:
gcloud functions deploy myFunction --entry-point=myFunction \
--region=us-central1 --project=my-gcp-project
This causes this error
Function 'myFunction' is not defined in the provided module.
Did you specify the correct target function to execute?
Could not load the function, shutting down.
Error: function terminated. Recommended action: inspect logs for termination reason.
Curiously enough, the deployment works if I rename function.js to index.js.
Does anyone know what I might be missing here?
Following the recommended structure, you need to import all methods from relevant modules and re-export them in the index.js file so that the Virtual image can find and bind them to the appropriate functions. Without this, your functions could be simply additional code that is used in other methods as Firebase has no way to tell the difference.
I suggest checking out the following documentation:
https://firebase.google.com/docs/functions/organize-functions#write_functions_in_multiple_files

#google-cloud/pubsub TypeError: Cannot read property 'status' of undefined

Installed the google cloud pub sub following instructions from the following link,
https://cloud.google.com/pubsub/docs/reference/libraries
Imported that as the following,
const { PubSub } = require("#google-cloud/pubsub");
const projectId = "projectXYZ";
const pubSubClient = new PubSub({ projectId });
export async function publishMessage(topicName, data) {
return await pubSubClient.topic(topicName).publish(JSON.stringify(data));
}
this leads to the following error,
Now, what I have observed is that the code below the import has no significance in this issue, since this error still appears even if I only import on the first line.
Am I missing something or required to install something more that just the package?
Any help is deeply appreciated.
Thank you in advance.
Library maintainer here! I'm guessing from the stack trace that you're using webpack on your app? Right now we don't have webpack and rollup support - there are a few weird things like what you see above. Your code looks okay. We'd like to get webpack and similar tools working at some point for server-side usage, but it hasn't gone very high on the priority list just yet. I think the previous assumption was that people were trying to use it in a web browser, which we don't recommend for a number of reasons (most prominently, security concerns with GCP credentials). But I've been seeing a lot of issues with users trying to use webpack on the server side to make their Cloud Functions more compact, which seems pretty legit. I'll bring it up in our next team sync.
If you're not using webpack, then that sounds like something that might be filed over at the issue tracker.

Detecting environment in GCloud Functions (Live/Emulator)

GCloud Functions are great and the emulator is a cool tool, but I'm having problems figuring out a way to detect wether I am running in a live mode or in the emulator.
Environment variables would come to mind, but I can't find a way to pass them along to the function when calling.
Runtime config might also be a solution but the emulator does not support this.
Easy fix would be using a param in the payload e.g. isDev: true and then check in the function:
if (data.isDev === true)
{ //use dev config }
else
{ // use live config }
but I'd keep that as a last resort solution.
Any other ideas on what could be a better solution?

How can I access the hapi plugin name?

I am working on node using hapi framework. New to this so I apologize if it comes out to be very basic.
I want to access a plugin name within the plugin files.
Use case is I want to access the plugin options from the server object.
So I can do server.registrations[plugin_name].options.
My workflow at that point (where I require the options) is not inside the register method ( I run an independent script that just needs to initialize the server, not start it; so no routes here) so not able to access the options from there, but I have the server object available.
Also, what is the correct way to expose the plugin options to other files within the plugin? If I need the options after a lot of nesting of files etc , it is very chaotic to keep passing the options object somehow from file to file, method to method.
Not really clear how to work this problem out.
Not a 100% sure what your question is but if you expose your options in a script (module) that can be required by both your plugin and any other files that you want to use the same options object that will allow you to access the same object in multiple places.
Please let me know if i understood correct, you can do like this.
const Plugins = require('./Plugins');
//Register All Plugins
server.register(Plugins, err => {
if (err){
server.error(`Error while loading plugins : ${err}`)
}else {
server.log('info', 'Plugins Loaded')
}
});

Changing the default Sails public folder

I know from experience that Sails creates a ./.tmp/public/ folder from where it serves all my files. I'd like to change it to just ./public/
I read a GitHub issue that said I could do it in config/local.js, but that doesn't seem to be working. Here's the GitHub issue: https://github.com/balderdashy/sails/issues/709
Is there a way to accomplish this? Thanks.
As to why I'm trying to do this, it's because I'm actually trying to work with Sails and Parse to build a simple test application. Since Parse looks in the ./public/ directory by default and I couldn't find a way to change this behaviour, I'm trying to configure Sails.
Create a file config/paths.js with the value:
module.exports.paths = {
'public': 'assets/'};
As in the mentioned issue report it can either be done in the .sailsrc file
{
"paths": {
"public": "bar/foo"
}
}
or if you launch sails in your own app (no using sails lift)
var Sails = require('sails');
Sails.lift({
paths: { public: 'mydir/pub' }, // relative to appDir
}, function(err, server) {});
If your using out of the box Grunt tasks, then you will need to go through and edit those tasks in <root>/tasks/config, there does not seem to be an global config variable for this, so you might have touch each file.
http://sailsjs.org/documentation/anatomy/my-app/tasks/config

Resources