Minimal Node.js Development Environment Using Docker Compose

Quick and painless setup of a basic local development environment for Node.js using docker-compose.

This is a quick tutorial on how to get a Node.js Docker container up and running for local development.

This approach does NOT require a Dockerfile and solves infamous insidious server response issues.  No more “localhost didn’t send any data” or “ERR_EMPTY_RESPONSE” or “127.0.0.1 didn’t send any data”.

You will need Docker Community Edition installed and running (aka Docker desktop) and exactly two files to fire up a Node.js app – “docker-compose.yml” and “app.js”.

local development environment for Node.js using docker-compose

docker-compose.yml

version: "3"
services:
    app:
        image: node:alpine
        volumes:
          - .:/app
        working_dir: /app
        ports:
          - 80:80
        command: node app.js

app.js

const http = require('http');
const hostname = '0.0.0.0';
const port = 80;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Live long and prosper!\n');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Then navigate to the newly-created folder using the console and type: “docker-compose up -d”

That’s all.
Now you can open your browser and access your app at “http://localhost

Connect to WooCommerce Rest API using Javascript

Do you  know, how to get data from WooCommerce via Rest API from the client, using JavaScript? Surprisingly, tutorials on this delicate subject are seldom found on page one of search results. Moreover, as of the moment of this writing WooCommerce documentation is also missing this information.

Warning: It is never a good idea to expose API authentication credentials.

Nevertheless, in rare cases when you do end up using this frivolous setup in production, make sure that your API key is for read only.

Below are a couple working code snippets – one using jQuery and one using vanilla async/await. Both get the job done:

const wooClientKey = 'ck_00000000000000000000000000';
const wooClientSecret = 'cs_00000000000000000000000000';
const wooUrl = 'https://yoursite.com/wp-json/wc/v3/products';

function basicAuth(key, secret) {
    let hash = btoa(key + ':' + secret);
    return "Basic " + hash;
}

let auth = basicAuth(wooClientKey, wooClientSecret);

function getData(url) {
    jQuery.ajax({
        url: url,
        method: 'GET',
        beforeSend: function (req) {
            req.setRequestHeader('Authorization', auth);
        }
    })
        .done(function (data) {
            console.log(data);
            return data;
        });
}

getData(wooUrl);

While the code itself is fairly self explanatory, here’s a quick step by step breakdown:

Const

The “consts” are merely script settings, which one can generate from within their WooCommerce  API section (WP Admin area).

basicAuth

This is a helper function, introduced for clarity.

The “Basic” HTTP authentication scheme transmits access credentials – username and password, encoded using base64 (defined in RFC 7617).

Thus, all basicAuth does is uses btoa() method to create a base64 encoded ASCII string  (<key>: <secret>) and return the resulting hash along with the “Basic” identifier.

Vanilla Javascript

const wooClientKey = 'ck_00000000000000000000000000';
const wooClientSecret = 'cs_00000000000000000000000000';
const wooUrl = 'https://yoursite.com/wp-json/wc/v3/products';

function basicAuth(key, secret) {
    let hash = btoa(key + ':' + secret);
    return "Basic " + hash;
}

let auth = basicAuth(wooClientKey, wooClientSecret);

async function getProducts() {
    try {
        const response = await fetch(wooUrl + 'products', {
            headers: {"Authorization": basicAuth(wooClientKey, wooClientSecret)}
        });
        return await response.json();
    }
    catch (error) {
        // catches errors both in fetch and response.json
        console.log(error);
    }
}
Отладка. Будет убрана потом