-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
40 lines (30 loc) · 874 Bytes
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import dotenv from "dotenv";
import express, { Request, Response } from "express";
import chalk from "chalk";
import bodyParser from "body-parser";
import boom from "express-boom";
import Printer from "./lib/printer";
const PORT = 3000;
const printer = new Printer("tcp://192.168.1.205:9100");
printer.connect().then((_p) => {
chalk.green("Printer setup done.");
});
const app = express();
// Add middleware
app.use(bodyParser.json());
app.use(boom());
app.post('/printer', async (req: Request, res: Response) => {
const { commands } = req.body;
if (printer == null) {
res.boom.badImplementation('Printer not ready. Try again later');
}
try {
await printer.executeCommands(commands);
} catch(err) {
return res.boom.badRequest(`Error executing commands: ${err}`)
}
res.send({
message: "Commands executed",
});
});
app.listen(PORT);