Products

Jspreadsheet and Express

This tutorial explains how to create a Jspreadsheet Server and Express running on the same application and port.

const server = require('@jspreadsheet/server');
const adapter = require('@jspreadsheet/redis');

const express = require('express');
const http = require('http');
const app = express();
const httpServer = http.createServer(app);

app.get('/test', (req, res) => {
    res.send('Hello world!');
});

// Load from ENV
require('dotenv').config();

// Jspreadsheet license
const license = {
    clientId: process.env.JSS_CLIENT,
    licenseKey: process.env.JSS_LICENSE
};

// Create Jspreadsheet Server
server({
    config: {
        cors: {
            origin: "*"
        },
    },
    port: 3000,
    server: httpServer,
    beforeConnect: async function(auth) {
        // Allow everyone
        return true;
    },
    beforeLoad: async function(guid, auth) {
        // Allow everything
        return true;
    },
    beforeChange: async function(guid, changes, auth) {
        // Allow everything
        return true;
    },
    load: async function(guid, auth, cachedConfiguration) {
        // Load the spreadsheet when is not already in cache
        if (! cachedConfiguration) {
            // Load the spreadsheet from the adapter
            cachedConfiguration = await adapter.load(guid, auth);
        }
        return cachedConfiguration;
    },
    change: async function(guid, changes, auth, onerror) {
        return await adapter.change(guid, changes, auth, onerror);
    },
    create: async function(guid, config, auth) {
        return await adapter.create(guid, config, auth);
    },
    destroy: async function(guid, auth) {
        return await adapter.destroy(guid, auth);
    },
    error: function(e) {
        console.error('Error', e)
    },
    license: license
});