Building a Node.js E-Commerce API for noobs – Express.js & Moltin

I’m developing locally using following configurations:

  • MacOS X
  • Node v8.0.0
  • npm v5.0.3
  • Chrome 58

What is Moltin?

I’m assuming that whoever reads this already knows what Express.js is good for, so I’m skipping it’s introduction. (if not have a glance at their website they’re describe it best themselves!)

The Moltin API on the other hand is not as known as express or nodejs itself I guess… 
But it’s awesome! They’ve got a simple, fast, reliable and well documented API that gives you an interface to a webshop dashboard where you can add products and all sorts of related things, manage orders, payment gateways and so forth..

Note: You need a Moltin account and a store to follow along! Don’t worry; it’s pretty easy to setup! moltin.com

Enough talking

Let’s get started!!!

I’ll init a project using npm

$ npm init

Create the starting file app.js:

// require the express module
const app = require('express')();
// Spin up a web server
app.listen(3000, function () {
console.log('Ecommerce sample listening on port 3000!');
});

Install the Express Module, Moltin, performance-now (because I’d like to test some stuff later on) and Nodemon CLI (so I don’t need to restart the server all the time manually) start the server:

$ npm i --save express moltin performance-now &&
$ npm i -g nodemon &&
$ nodemon

Voilà.. Express is up and running!

Test performance when using Moltin API

Since I’m planning to use the Moltin API for multiple shops, I’m wondering whether and how to use multiple Moltin instances in one Node processes?

So, normally you would add something to the app.js like:

// require the moltin module I just installed
const moltin = require('moltin');
// configure the store I created at moltin
const Moltin = moltin.gateway({
client_id: 'blabla_client_id',
client_secret: 'blabla_client_secret',
});
// authenticate against moltin using our gateway and log moltins   // response
Moltin.Authenticate()
.then(response => {
console.log('authenticated', response);
});
// Get the product => none bc I didn't create any
const products = Moltin.Products.All(); // products: {data: []}

But what if you tried to connect multiple Moltin stores in one node? 
Authenticate against the Moltin API on every call again?
I didn’t and yet don’t really know what kind of effect that might have on performance.. so I tried to get an idea of how fast this actually is..

const moltin = require('moltin');
const now = require('performance-now');
const Moltin = moltin.gateway({
client_id: 'blabla_client_id',
client_secret: 'blabla_client_secret',
});
Moltin.Authenticate()
.then(response => {
console.log('authenticated', response);
});
// start the time
const start = now();
Moltin.Products.All()
.then(() => {
const end = now(); // stop the time and log it
console.log((start-end).toFixed(3)); //returned ~360

});

So authenticating once and then calling the Moltin API for products is pretty fast; about 360ms average!
For testing how fast this stuff would be when we need to authenticate everytime when calling the Moltin API..
For this purpose I’m going to change the code and make use of express as a webserver and routes that’ll trigger those Moltin functions:

const app = require('express')();
const moltin = require('moltin');
const now = require('performance-now');
/*---------------Routes-----------------*/
app.get('/moltin', (req, res) => {
const start = now();
// same same as before
let Moltin = moltin.gateway({
client_id: 'blabla_client_id',
client_secret: 'blabla_client_secret',
});
// first authenticate and then fetch the products from Moltin
Moltin.Authenticate()
.then(() => {
Moltin.Products.All()
.then(prods => {
const end = now();
console.log('TIME:', (start-end).toFixed(3)); // ~500

res.send(prods);
});
});
});
app.listen(3000, function () {
console.log('Ecommerce sample listening on port 3000!');
});

I can then just run in a new terminal window:

$ curl http://localhost:3000/moltin

Which will console.log() the times to the other window…
Well, the results range from about 460ms to 640ms… which I think is pretty fast!
I’m confident that I can go with authenticating before every call; at least I think it’s a legit solution (?).
I will write a middleware, that takes care of that!

Middleware

const app = require('express')();
const moltin = require('moltin');
/*------------Middleware---------------*/
// same gateway && authentication.. just wrapped in a function
const authenticateMoltin = function (req, res, next) {
let Moltin = moltin.gateway({
client_id: 'blalba_client_id',
client_secret: 'blabla_client_secret',
});
Moltin.Authenticate()
.then(() => {
req.Moltin = Moltin;
next();
});
};
// tell express to use the auth function as middleware on every    // route
app.use(authenticateMoltin);
/*---------------Routes-----------------*/
app.get('/moltin', (req, res) => {
let { Moltin } = req;
 Moltin.Products.All()
.then(prods => {
res.send(prods);
});
});
app.listen(3000, function () {
console.log('Ecommerce sample listening on port 3000!');
});

Simple, moved the authentication function from the route into an own function and then told express to use it as middleware “app.use(middleware)” . Now every route on the app runs the Moltin Authentication first!

Alright, got the bases set up!
Now let’s get into real code and create the routes I’ll use to get our products, carts and so on!

Intro
You are on the 2nd pt.
Part Three here
Part Four Here