1.1.0 • Published 1 year ago

sponge-clientjs v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Sponge ClientJS

A simple package that allows you to to run javascript on the client side from the backend in express.

Its a very simple package which may be trivial, but I find it helpful as you can execute different scripts on the client side.

Installation

npm i sponge-clientjs

Usage:

const {clientJS, jsSnippets} = require('sponge-clientjs');

clientJS(engine, type, script, response); 

Example:
clientJS('ejs', '', `alert('Hello world)`, res); 

An example:

server.get('/', (req, res) => {
    var text = 'abcwe'
    if(typeof(text) === `string`){
        clientJS('', '', `alert('This is a string')`, res);
    }
    res.sendFile(__dirname + '/index.html')
});

Or you can use the jsSnippets:

server.get('/', (req, res) => {
    clientJS('', '', jsSnippets.alert('Hello world'), res);
});

However, if you want to use DOM properties such as getElementById().style or .innnerHTML you will need EJS.

Here is an example:

const express = require('express');
const server = express();
const {clientJS, jsSnippets} = require('sponge-clientjs');

server.set('view engine', 'ejs')

let scriptjs;

server.get('/', (req, res) => {
    let text = 'sdsd'
    if(typeof(text) === 'string'){
        scriptjs = clientJS('ejs', '', jsSnippets.document.getElementById('heading1').style('color', 'blue'), res);
    }
    res.render('index', {'clientjs': scriptjs});
});


//run multiple lines of code:
server.get('/', (req, res) => {
    let text = 'sdsd'
    if(typeof(text) === 'string') {
        scriptjs = clientJS('ejs', '', [
            jsSnippets.alert('Hello'),
            jsSnippets.document.getElementById('heading1').style('color', 'blue')
        ], res);
    }
    res.render('index', {'clientjs': scriptjs});
});

EJS code:

<h1 id="heading1">New text</h1>
<%- clientjs %>

Hope it helps!

License

ISC

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago