0

NodeJS isn't working properly

I'm working on a very simple project in which I use a CSV to display changing data on a screen, For this I'm using NodeJS, but I can't get it to work properly on a XT2145, I've lost track of everything I've tried so far I've been able to dispaly the HTML with no Nodejs functionality:

Here's the HTML:

<body>
  <input id="input-text" type="text" placeholder="Enter product name">
  <button id="load-product">Load Product</button>
  <h1 class="product-name" id="product-name"></h1>
  <h3 class="brand" id="brand"></h3>
  <p class="price" id="price"></p>
  <p class="description" id="description"></p>
  <script>
      document.getElementById('load-product').addEventListener('click', function() {
          const productName = document.getElementById('input-text').value;
          fetch(`/products/${productName}`)
              .then(response => response.json())
              .then(data => {
                  if (data.error) {
                      alert(data.error);
                  } else {
                      document.getElementById('product-name').textContent = data.ProductName;
                      document.getElementById('brand').textContent = data.Brand;
                      document.getElementById('price').textContent = `$${data.Price}`;
                      document.getElementById('description').textContent = data.Description;
                  }
              })
              .catch(error => console.error('Error:', error));
      });
  </script>
</body>

and this is the NodeJS:

const express = require('express');
const fs = require('fs');
const csv = require('csv-parser');

const app = express();
const PORT = 3000;
app.use(express.static('public'));
app.get('/products/:name', (req, res) => {
  const results = [];
  const productName = req.params.name.toLowerCase();
  fs.createReadStream('products.csv')
      .pipe(csv())
      .on('data', (data) => results.push(data))
      .on('end', () => {
          const product = results.find(p => p.ProductName.toLowerCase() === productName);
          if (product) {
              res.json(product);
          } else {
              res.status(404).json({ error: 'Product not found' });
          }
      });
});
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
CSV:
ProductName,Brand,Price,Description,image
Focus,Poly,50,Wireless Headset,focus.jpeg
Blackwire,Poly,60,Wired Headset,black.png
on the input bar you write either "focus" or "blackwire" and you get some data.

I've tried using the example but I also couldn't run it.

1 comment

  • 0
    Avatar
    Johan

    Just to confirm, you are using some sort of package manager (for example WebPack) that includes your NPM packages to your build? In other words is the code from `csv-parser` being shipped to the BrightSign when you put your code on there? 

    Not sure if csv-parser is a standard node package on BrightSign but normally in Node projects it isn't.

Please sign in to leave a comment.