简体   繁体   English

如何为escpos协议构建angular和node js?

[英]How to build angular and node js together for escpos protocol?

sorry for my english.对不起我的英语不好。 I have angular front-end, node js server, thermal printer.我有 angular 前端,节点 js 服务器,热敏打印机。 I had to communicate with thermal printers from angular using esc/pos protocol, so I made a node js server.我不得不使用 esc/pos 协议从 angular 与热敏打印机通信,所以我做了一个节点 js 服务器。 Now I do a request from angular to node and then node communicates with the printer (using ip and port).现在我从 angular 向节点发出请求,然后节点与打印机通信(使用 ip 和端口)。 How can I build angular and node together?如何一起构建 angular 和节点? So when I deploy it I don't have to start node server every time?所以当我部署它时,我不必每次都启动节点服务器? Thanks谢谢

I suppose you are refering to run it locally.我想您指的是在本地运行它。 There are several ways:有几种方法:

  1. In windows 10, for example, you can run a nodejs "server" like a window service.例如,在 windows 10 中,您可以像 window 服务一样运行 nodejs“服务器”。 So it runs on startup.所以它在启动时运行。 (take a look at this repo Forever tool ) (看看这个 repo Forever 工具
  2. You can use electron js: first of all generate your build of Angular project: ng b --prod : this will generate the dist folder with your index.html and necessary scripts.您可以使用 electron js:首先生成 Angular 项目的构建: ng b --prod :这将使用您的 index.html 和必要的脚本生成 dist 文件夹Now you can serve this static folder from your node js script: for example, with the help of Express js .现在,您可以从节点 js 脚本中提供此 static 文件夹:例如,借助Express js

 var express = require("express"); var app = express(); app.use(express.static("/dist/myAngularProject")); //Add your escpos logic, ecc.. app.listen(3001, function () { console.log("Server listening on port 3001;"); });

Now if you run the comand: node index.js , you should see your angular app.现在,如果您运行命令: node index.js ,您应该会看到您的 angular 应用程序。 The next step is very simple: start a new Electron js application, and in your main.js load your server URL:下一步非常简单:启动一个新的Electron js应用程序,并在您的 main.js 中加载您的服务器 URL:

 const { app, BrowserWindow } = require('electron') require("./server/myNodeJsServerIndex.js") let win function createWindow () { win = new BrowserWindow({ width: 950, height: 750, fullscreen: true, webPreferences: { nodeIntegration: true, devTools:false } }) //my node js server win.loadURL(`http://localhost:3001`); win.on('closed', () => { win = null }) } app.on('ready', createWindow) app.on('window-all-closed', () => { if (process.platform.== 'darwin') { app.quit() } }) app,on('activate', () => { if (win === null) { createWindow() } })

With require("./server/myNodeJsServerIndex.js") and win.loadURL( http://localhost:3001 );使用require("./server/myNodeJsServerIndex.js")win.loadURL( http://localhost:3001 ); , it means that every time that you start the electron client, your nodejs server will start automatically. ,这意味着每次启动 electron 客户端时,您的 nodejs 服务器都会自动启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM