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.
I've tried using the example but I also couldn't run it.