vizam's avatar
Level 1

how to run NodeJS from Laravel controller

I have a Laravel project to generate pwa icons when uploading an image, for that I have used some NodeJS script. It is working by running

node server.js

in terminal ( server.js my file name ).

but my purpose is to run that script after uploading image from controller.

how could I do that?

I have used

exec

but it not working.

server.js is located in public/node/server.js

also tried, $process = new Process(['node', 'server.js']); $process->run();

if (!$process->isSuccessful()) { throw new ProcessFailedException($process); }

echo $process->getOutput();

but not working. getting this error

The command "node server.js" failed. Exit Code: 1(General error) Working directory: E:\vismayavc\LaravelNodeCombo\public Output: ================ Error Output: ================ 'node' is not recognized as an internal or external command, operable program or batch file

please someone help me to solve this.

0 likes
7 replies
vizam's avatar
Level 1

Thank you for your response.

I got this after adding your solution.

The command ""C:\Program Files\nodejs\node.EXE" "node/server.js"" failed. Exit Code: 1(General error) Working directory: E:\vismayavc\LaravelNodeCombo\public Output: ================ generating different icons sizes ./iconslist/icon-192x192.png ./iconslist/icon-144x144.png ./iconslist/icon-192x192.png ./iconslist/icon-16x16.png ./iconslist/icon-32x32.png ./iconslist/icon-96x96.png ./iconslist/icon-128x128.png ./iconslist/icon-192x192.png ./iconslist/icon-256x256.png ./iconslist/icon-384x384.png ./iconslist/icon-512x512.png ./iconslist/icon-57x57.png ./iconslist/icon-60x60.png ./iconslist/icon-72x72.png ./iconslist/icon-76x76.png ./iconslist/icon-120x120.png ./iconslist/icon-128x128.png ./iconslist/icon-144x144.png ./iconslist/icon-152x152.png ./iconslist/icon-167x167.png ./iconslist/icon-180x180.png ./iconslist/icon-192x192.png ./iconslist/icon-256x256.png ./iconslist/icon-384x384.png ./iconslist/icon-512x512.png generating different splashscreen sizes ./iconslist/splash-1136x640.png ./iconslist/splash-640x1136.png ./iconslist/splash-2436x1125.png ./iconslist/splash-1125x2436.png ./iconslist/splash-1792x828.png ./iconslist/splash-828x1792.png ./iconslist/splash-1334x750.png ./iconslist/splash-750x1334.png ./iconslist/splash-2208x1242.png ./iconslist/splash-1242x2208.png ./iconslist/splash-1242x2688.png ./iconslist/splash-2688x1242.png ./iconslist/splash-2732x2048.png ./iconslist/splash-2048x2732.png ./iconslist/splash-2224x1668.png ./iconslist/splash-1668x2224.png ./iconslist/splash-2388x1668.png ./iconslist/splash-1668x2388.png ./iconslist/splash-2048x1536.png ./iconslist/splash-1536x2048.png Your node server start.... Your node server start successfully.... Error Output: ================ events.js:377 throw er; // Unhandled 'error' event ^ Error: listen UNKNOWN: unknown error 0.0.0.0:8001 at Server.setupListenHandle [as _listen2] (net.js:1314:21) at listenInCluster (net.js:1379:12) at Server.listen (net.js:1465:7) at Function.listen (E:\vismayavc\LaravelNodeCombo\node_modules\express\lib\application.js:618:24) at Object.<anonymous> (E:\vismayavc\LaravelNodeCombo\public\node\server.js:149:5) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) Emitted 'error' event on Server instance at: at emitErrorNT (net.js:1358:8) at processTicksAndRejections (internal/process/task_queues.js:82:21) { code: 'UNKNOWN', errno: -4094, syscall: 'listen', address: '0.0.0.0', port: 8001 }
Sinnbeck's avatar

@vizam Is the server.js actually trying to start a webserver?! I just thought it was bad naming..

vizam's avatar
Level 1

@Sinnbeck my aim is to generate icons and add those to a folder by running that script and show "Icon generated successfully" msg in Laravel blade

Sinnbeck's avatar

@vizam Can you perhaps show that server.js? It sounds like it is trying to start a webserver

unknown error 0.0.0.0:8001 at Server.setupListenHandle

vizam's avatar
Level 1

@Sinnbeck yes.it starting a webserver

i have changed server.js file

const fs = require('fs');
var Jimp = require('jimp');

class Link {
    constructor({ size, type, rel = 'icon', showSizes = true, relName = 'rel' }) {
        this.size = size;
        this.rel = rel;
        this.type = type
        this.sizes = `${this.size}x${this.size}`;
        this.relName = relName;
        this.showSizes = showSizes;
    }

    pathToIcon() {
        return `./iconslist/icon-${this.sizes}.png`
    }

    generateLinkTag() {
        const sizesString = this.showSizes ? `sizes="${this.sizes}"` : '';
        const typeString = this.type ? `type="${this.type}"` : '';
        return `<link ${this.relName}="${this.rel}" ${typeString} ${sizesString} href="${this.pathToIcon()}">`
    }

}

const LinkRel = {
    IE: 'msapplication-TileImage',
    CHROME: 'icon',
    APPLE: 'apple-touch-icon'
};

const sizes = [
    new Link({ size: 192, type: 'image/png', showSizes: false }),
    new Link({ size: 144, rel: LinkRel.IE, relName: 'name', type: 'image/png', showSizes: false }),
    new Link({ size: 192, rel: LinkRel.APPLE, showSizes: false }),
    new Link({ size: 16, type: 'image/png', }),
    new Link({ size: 32, type: 'image/png', }),
    new Link({ size: 96, type: 'image/png', }),
    new Link({ size: 128, type: 'image/png', }),
    new Link({ size: 192, type: 'image/png', }),
    new Link({ size: 256, type: 'image/png', }),
    new Link({ size: 384, type: 'image/png', }),
    new Link({ size: 512, type: 'image/png', }),
    new Link({ size: 57, rel: LinkRel.APPLE }),
    new Link({ size: 60, rel: LinkRel.APPLE }),
    new Link({ size: 72, rel: LinkRel.APPLE }),
    new Link({ size: 76, rel: LinkRel.APPLE }),
    new Link({ size: 120, rel: LinkRel.APPLE }),
    new Link({ size: 128, rel: LinkRel.APPLE }),
    new Link({ size: 144, rel: LinkRel.APPLE }),
    new Link({ size: 152, rel: LinkRel.APPLE }),
    new Link({ size: 167, rel: LinkRel.APPLE }),
    new Link({ size: 180, rel: LinkRel.APPLE }),
    new Link({ size: 192, rel: LinkRel.APPLE }),
    new Link({ size: 256, rel: LinkRel.APPLE }),
    new Link({ size: 384, rel: LinkRel.APPLE }),
    new Link({ size: 512, rel: LinkRel.APPLE }),
];


class IOSSplashscreen {
    constructor({ width, height, deviceWidth, deviceHeight, orientation = 'landscape' }) {
        this.width = width;
        this.height = height;
        this.deviceWidth = deviceWidth;
        this.deviceHeight = deviceHeight;
        this.orientation = orientation;
    }

    pathToIcon() {
        return `./iconslist/splash-${this.width}x${this.height}.png`
    }


    generateLingTagForSplashScreenInIOS() {
        return `<link rel="apple-touch-startup-image"
        media="screen and (device-width: ${this.deviceWidth}px) and (device-height: ${this.deviceHeight}px) and (-webkit-device-pixel-ratio: 2) and (orientation: ${this.orientation})"
        href="./iconslist/splash-${this.width}x${this.height}.png" />`
    }

}

const ios_splashscreen = [
    new IOSSplashscreen({ width: 1136, height: 640, deviceWidth: 320, deviceHeight: 568 }),
    new IOSSplashscreen({ width: 640, height: 1136, deviceWidth: 320, deviceHeight: 568, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 2436, height: 1125, deviceWidth: 375, deviceHeight: 812 }),
    new IOSSplashscreen({ width: 1125, height: 2436, deviceWidth: 375, deviceHeight: 812, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 1792, height: 828, deviceWidth: 414, deviceHeight: 896 }),
    new IOSSplashscreen({ width: 828, height: 1792, deviceWidth: 414, deviceHeight: 896, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 1334, height: 750, deviceWidth: 375, deviceHeight: 667 }),
    new IOSSplashscreen({ width: 750, height: 1334, deviceWidth: 375, deviceHeight: 667, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 2208, height: 1242, deviceWidth: 414, deviceHeight: 736 }),
    new IOSSplashscreen({ width: 1242, height: 2208, deviceWidth: 414, deviceHeight: 736, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 1242, height: 2688, deviceWidth: 414, deviceHeight: 896 }),
    new IOSSplashscreen({ width: 2688, height: 1242, deviceWidth: 414, deviceHeight: 896, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 2732, height: 2048, deviceWidth: 1024, deviceHeight: 1366 }),
    new IOSSplashscreen({ width: 2048, height: 2732, deviceWidth: 1024, deviceHeight: 1366, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 2224, height: 1668, deviceWidth: 834, deviceHeight: 1112 }),
    new IOSSplashscreen({ width: 1668, height: 2224, deviceWidth: 834, deviceHeight: 1112, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 2388, height: 1668, deviceWidth: 834, deviceHeight: 1194 }),
    new IOSSplashscreen({ width: 1668, height: 2388, deviceWidth: 834, deviceHeight: 1194, orientation: 'portrait' }),

    new IOSSplashscreen({ width: 2048, height: 1536, deviceWidth: 768, deviceHeight: 1024 }),
    new IOSSplashscreen({ width: 1536, height: 2048, deviceWidth: 768, deviceHeight: 1024, orientation: 'portrait' }),
]

const iconPath = '../image/logo.png';
const pwaLink = 'pwa.html';
sizes.forEach((link) => {
    const linkFileName = link.pathToIcon();
    Jimp.read(iconPath)
        .then(logo => {
            fs.appendFileSync(pwaLink, link.generateLinkTag());
            return logo
                .resize(link.size, link.size)
                .write(linkFileName);
        }, err => console.error(err));
});


ios_splashscreen.forEach((splash) => {
    const splashFileName = splash.pathToIcon(splash.width, splash.height);
    Jimp.read(iconPath)
        .then(logo => {
            fs.appendFileSync(pwaLink, splash.generateLingTagForSplashScreenInIOS());
            return logo
                .resize(splash.width, splash.height)
                .write(splashFileName);
        }, err => console.error(err));
});

there is no error , but its not working

vizam's avatar
Level 1

exec("node server.js", $output);

by using this icons are generated.

also getting this

Symfony\Component\ErrorHandler\Error\FatalError
Maximum execution time of 60 seconds exceeded

Please or to participate in this conversation.