Passing a JSON from another js file with Express and EJS

I am working on a project to manage printers with CUPS command-line, I have multiple "api’s" that make different thinks, for now i just want so see my parsed JSON result in a view, but I’m clueless on how to pass that value trough Express and then render it in a EJS view:

my api:

const spawnSync = require("child_process").spawnSync;
const parseStdout = require('../utils/utils.js');

function lpstat(){
let printerList = spawnSync("lpstat -p", {
    timeout: 10000,
    encoding: "utf-8",
  });
 
  let parsedList = parseStdout(printerList);
  
  let onlyPrinterList = parsedList.filter(function (line) {
    return line.match(line.match(/^printer/) || line.match(/^impressora/));
  });
  
  let onlyPrinterNames = onlyPrinterList.map(function (printer) {
    return printer.match(/(?: \S+)/)[0].trim();
  });
  process.on('exit', (code) => {
    process.kill();
  });
  //this is what i want to pass to the view
   return JSON.stringify(onlyPrinterNames);
}

my app.js

const express = require('express');
const app = express();

app.listen(3000);
app.set('view engine', 'ejs');


app.get('/lpstat',(req,res) => {
//what should i use here?
    res.render('lpstat')
});

my lpstat.ejs

<html lang="en">
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>lpstat</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
    <p>lpstat result:</p>
   <%= what should i use here?%>
</body>
</html>

45 thoughts on “Passing a JSON from another js file with Express and EJS”

  1. The second parameter in res.render defines the data that is given to your templates:

    app.get('/lpstat',async (req,res) => {
        // Call your api here to fill the variable
        const printers = await callApi()
        res.render('lpstat', {
           printers
        })
    });
    

    You will be able to use this in your ejs template then

    <p>lpstat result:</p>
    <%= printers %>
    

    You will need to replace callApi with whatever function is used to fetch your data. I used async/await for a simpler answer, can be done with callbacks as well.

    Reply
  2. community bell advocates community health center of pinellas , positive and negative battery cables. community bank siren wi lyrica side effects lyrica 150 mg , community acquired pneumonia pediatrics guidelines climax community definition biology quizlet x ray before positive pregnancy test , community action partnership clark county ohio community college killeen tx? community action york pa community bible church palmyra, community health center yakima community action west palm beach.
    positive reinforcement and positive punishment , positive pregnancy test how many days after ovulation friends letter board a community definition biology.

    Reply
  3. office software knowledge drawing program website to buy autocad 2021 office software without subscription. software 2020 fusion office 2010 software free download full version engeeeneringu#$sssaunnplus , freesat v8 software download 2020. e-office software free download, office software free for mac software similar to 2020 design antivirus software 2020 best. office planning software free software 2020 design, software 2020 for pc.

    Reply

Leave a Comment