首页 > 解决方案 > 如何打开两个单独的 nw.js 应用程序

问题描述

我正在构建一个服务器和一个客户端 nwjs 应用程序,但我无法同时打开两者。我想知道是否有任何方法可以做到这一点。我npm run dev在我打开的两个 VS Code 上运行,但是当我在第二个应用程序上运行此命令时,它根本不会打开(无论哪个是我想运行的第二个应用程序)。我尝试构建客户端应用程序并运行它,在它运行服务器应用程序但它是相同的之后,第二个应用程序将无法启动。

这是我在两个应用程序中的 package.json 文件,我不知道这是否有帮助。只有应用程序中的名称不同(nwjs_client 和 nwjs_server)

{
  "name": "nwjs_server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nw src/",
    "prod": "nwbuild --platforms win32,win64,osx64,linux32,linux64 --buildDir dist/ src/"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nw": "^0.49.1"
  }
}

我愿意接受任何答案,我什至不知道是否可以运行 2 个不同的 nwjs 应用程序。

标签: npmpackage.jsonnode-webkit

解决方案


I'm confused by what you are trying to do.

  • If you are trying to run an NW.js that, instead of loading directly from a index.html, it loads displays a page from a local webserver:
    1. Create an server.js that spins up a local web server on a specific port (like 4263 or something not super common).
    2. If you need any node_modules for this (like express) make sure it is a dependency and not a devDependency.
    3. Set your "main" in the package.json to "http://localhost:4263 using the same port as the server
    4. set your "node-main" to "server.js", this will run in the node context before your window is displayed when starting the app.
    5. Set your "node-remote" to "http://localhost:4263" using the same port also. This will allow Node commands to run on that URL when loaded in NW.js.
  • If you are wanting to run two commands at the same time you can:
    1. npm install --save-dev concurrently wait-on. This will install two devDeps
    2. Set your npm script to "start": "concurrently \"npm run serve\" \"wait-on http://localhost:4263 && nw .\""
    3. This will run your npm run serve command, which presumably spins up a local webserver for development, if using something like webpack, this could take a minute. Then it waits until localhost:4263 actually returns a response. Then it launches NW.js

concurrently will also let you run any two (or more) commands at the same time.


推荐阅读