CO2.js comes with a handy function which lets you check if one domain or an array of domains is served from a green web host. Under the hood, this function uses the Greencheck API to return results.
In this tutorial, you will install CO2.js in a Node environment. Then, you will check the green hosting status of one or more domains.
You can follow along with this tutorial in your local development environment, or by using the button below to launch the project in Gitpod.
If you are following along this tutorial locally, you will need to have the following setup on your machine:
If you are following along using the Gitpod starter template, you can skip this section.
Otherwise, create a new folder locally called co2js-node
and navigate into that folder. Then, initialise NPM.
mkdir co2js-node
cd co2js-node
npm init -y
Next, create an hosting.js
file, and open it in your code editor of choice. We will write the code for this tutorial inside the hosting.js
file.
Inside your project folder, run the following command to install CO2.js as a dependency.
npm install @tgwf/co2
In your project’s hosting.js
file, add the following code to include the CO2.js hosting module in your code.
const { hosting } = require("@tgwf/co2");
The hosting module includes a check()
function. We will be using this to perform our green hosting checks.
To check if a single domain is green hosted, you can pass the following parameters into the check
function:
string
the website domain you want to check for green hosting.string
Optional since v0.14.2 the name of the project, product, or app which is performing the check.When checking a single domain, this function returns a boolean
response (true
for green hosted, false
for not).
Adding the code below to the hosting.js
file allows us to check if the domain google.com
is served from a green web host.
hosting.check("google.com", "myGreenWebApp").then((result) => {
console.log(result);
});
Running the code above returns the following result:
node hosting.js
# Output:
# true
To check if more than one domain is green hosted, you can pass the following parameters into the check
function:
array
an array of strings representing website domain you want to check for green hosting.string
Optional since v0.14.2 the name of the project, product, or app which is performing the check.When checking multiple domains, this function returns an array
of any green domains that are found.
Adding the code below to the hosting.js
file allows us to check if the domains google.com
, facebook.com
, and twitter.com
are served from a green web hosts.
const domains = ["google.com", "facebook.com", "twitter.com"];
hosting.check(domains, "myGreenWebApp").then((result) => {
console.log(result);
});
Running the code above returns the following result:
node hosting.js
# Output:
# ['google.com', 'facbook.com']
You now know how to use CO2.js to check one or more domains for green hosting.
The domain/s passed to the check()
function must not include any protocol, port, or path information.
climateaction.tech
Acceptedhttps://climateaction.tech
Incorrectclimateaction.tech/events
IncorrectWhen a green hosting check is performed for a domain, we will return either green: true
(it's a green host) or green: false
(not a green host).
If you're expecting a green result, but not getting it, there are a few reasons why this might be happening. We've covered these in our FAQ - Why does my website show up as grey in the Green Web Checker?.