custom-scripts/mccs-it/thermal/app.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-09-06 23:26:30 +00:00
const express = require('express');
const ThermalPrinter = require('node-thermal-printer').printer;
const Types = require('node-thermal-printer').types;
const printer = require('printer');
const app = express();
const port = 3000;
// Middleware to serve static files (HTML)
app.use(express.static('public'));
// Setup thermal printer
const thermalPrinter = new ThermalPrinter({
type: Types.STAR, // Replace with the correct printer type (e.g., STAR, EPSON)
interface: 'printer:TSP100', // Printer name as recognized by the OS
driver: printer, // Use node-printer driver
});
// Test print function
app.post('/test-print', async (req, res) => {
try {
// Initialize the printer
const isConnected = await thermalPrinter.isPrinterConnected();
if (!isConnected) {
return res.status(500).json({ message: 'Printer not connected' });
}
// Print a test line
thermalPrinter.println('This is a test print');
thermalPrinter.cut();
// Execute the print job
await thermalPrinter.execute();
res.json({ message: 'Print Success' });
} catch (error) {
console.error('Print Error:', error);
res.status(500).json({ message: 'Print Error', error: error.message });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});