node express | express rpc wrapper | directory to html | Search

This Express.js server code sets up endpoints for OAuth2 authentication, Eloqua, and Zuora, including authorization, token requests, data imports, synchronizations, and exports. Although the code is functional, many endpoints return sample responses, simulating interactions with the respective systems without actual data processing or interactions.

Run example

npm run import -- "zuora eloqua express mock"

zuora eloqua express mock

var importer = require('../Core')
var bodyParser = require('body-parser');
var express = require('express');

var selenium = express();
var server = require('http').createServer(selenium);
selenium.use(bodyParser.json());    // to support JSON-encoded bodies
selenium.use(bodyParser.urlencoded({// to support URL-encoded bodies
    extended: true
}));

var router = express.Router();


// auth endpoints
router.post('/auth/oauth2/authorize', (req, res) => { 
    res.send('');
});

router.post('/auth/oauth2/token', (req, res) => {
    res.send({
        "access_token": "access_token",
        "token_type": "bearer",
        "expires_in": 28800,
        "refresh_token": "refresh_token",
    })
});


// eloqua endpoints
router.post('/bulk/2.0/contacts/imports', (req, res) => {
    res.send({
        uri: '/imports/1234'
    });
});
router.post('/bulk/2.0/imports/:importId/data', (req, res) => {
    res.status(204);
    res.send({
        uri: '/imports/1234'
    });
});
router.post('/bulk/2.0/syncs', (req, res) => {
    res.send({
        uri: '/sync/1234'
    });
});
var alternateImportStatus = true;
router.get('/bulk/2.0/sync/:syncId', (req, res) => {
    res.send({
        status: alternateImportStatus ? 'active' : 'success',
    });
    alternateImportStatus = !alternateImportStatus;
});


// eloqua custom data object
// as opposed to /bulk/2.0/customobjects/imports which just lists all the imports that have been performed on the CDO
router.post('/bulk/2.0/customobjects/:objectId/imports', (req, res) => {
    res.send({
        uri: '/imports/1234'
        // might be? /customobjects/:objectId/imports/:importId
    });
});




// zuora endpoints
router.post('/object/export', (req, res) => {
    res.send({
        Id: '1234'
    });
});
var alternateExportStatus = true;
router.get('/object/export/:exportId', (req, res) => {
    res.send({
        Status: alternateExportStatus ? 'Processing' : 'Completed',
        FileId: '1234'
    });
    alternateExportStatus = !alternateExportStatus;
});
router.get('/files/:fileId', (req, res) => {
    res.send(`RatePlanCharge.Description,Account.Id
support,123456
premium,654321
`);
});

selenium.use(router);
var app;
function restart() {
    if(typeof app !== 'undefined') {
        app.close();
    }
    app = server.listen(18888).on('error', e => {
        if(e.code !== 'EADDRINUSE') {
            throw e;
        }
    });
    return app;
}
restart();
app.restart = restart;
module.exports = app;

process.on ('SIGTERM', () => {app.close(); process.exit(0)});
process.on ('SIGINT', () => {app.close(); process.exit(0)});

What the code could have been:

const importer = require('../Core');
const bodyParser = require('body-parser');
const express = require('express');
const helmet = require('helmet'); // added for security
const morgan = require('morgan'); // added for logging
const { createServer } = require('http');

// Initialize express app
const app = express();

// Set middlewares
const server = createServer(app);
app.use(helmet()); // security
app.use(morgan('combined')); // logging
app.use(bodyParser.json()); // support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // support URL-encoded bodies
    extended: true
}));

// Initialize router
const router = express.Router();

// TODO: implement authentication using a library like passport.js
router.post('/auth/oauth2/authorize', (req, res) => {
    res.status(200).json({ message: 'Authorized' });
});

router.post('/auth/oauth2/token', (req, res) => {
    res.status(200).json({
        access_token: 'access_token',
        token_type: 'bearer',
        expires_in: 28800,
        refresh_token:'refresh_token',
    });
});

// ELOQUA endpoints
router.post('/bulk/2.0/contacts/imports', (req, res) => {
    res.status(201).json({
        uri: '/imports/1234',
        message: 'Import created',
    });
});

router.post('/bulk/2.0/imports/:importId/data', (req, res) => {
    res.status(204).send({ uri: '/imports/1234' });
});

router.post('/bulk/2.0/syncs', (req, res) => {
    res.status(201).json({
        uri: '/sync/1234',
        message: 'Sync created',
    });
});

let alternateImportStatus = true;
router.get('/bulk/2.0/sync/:syncId', (req, res) => {
    res.status(200).json({
        status: alternateImportStatus? 'active' :'success',
    });
    alternateImportStatus =!alternateImportStatus;
});

// ELOQUA custom data object
router.post('/bulk/2.0/customobjects/:objectId/imports', (req, res) => {
    res.status(201).json({
        uri: '/imports/1234',
        message: 'Import created',
    });
});

// ZUORA endpoints
router.post('/object/export', (req, res) => {
    res.status(201).json({
        Id: '1234',
        message: 'Export created',
    });
});

let alternateExportStatus = true;
router.get('/object/export/:exportId', (req, res) => {
    res.status(200).json({
        Status: alternateExportStatus? 'Processing' : 'Completed',
        FileId: '1234',
    });
    alternateExportStatus =!alternateExportStatus;
});

router.get('/files/:fileId', (req, res) => {
    res.status(200).send(`RatePlanCharge.Description,Account.Id
support,123456
premium,654321
`);
});

// Use router
app.use(router);

// Restart server function
function restart() {
    if (app) {
        app.close();
    }
    app = server.listen(18888).on('error', (e) => {
        if (e.code!== 'EADDRINUSE') {
            throw e;
        }
    });
    return app;
}

// Initialize server on start
restart();

// Export app
app.restart = restart;
module.exports = app;

// Exit handlers
process.on('SIGTERM', () => {
    app.close();
    process.exit(0);
});
process.on('SIGINT', () => {
    app.close();
    process.exit(0);
});

Code Breakdown

This code sets up an Express.js server with several endpoints for different systems: OAuth2 authentication, Eloqua (a marketing automation platform), and Zuora (a cloud-based billing and revenue management solution).

Requires and Setup

The code starts by requiring necessary modules:

It then sets up an Express.js application, creating an HTTP server, and enabling JSON and URL-encoded body parsing.

Endpoints

The code defines several endpoints using Express.js router:

OAuth2 Endpoints

Eloqua Endpoints

Zuora Endpoints

Note that many of these endpoints return sample responses and do not perform actual data processing or interactions with the respective systems.