node.js - 有什么办法可以从 Nuxt+Node 项目中生成可执行文件 (.exe) 吗?

标签 node.js nuxt.js

我有一个 NuxtJS 项目,它需要一个 NodeJS 程序在后面运行以实现某些功能和逻辑。项目结构如下:

api
assets
components
layouts
middleware
pages
plugins
server
static
store
nuxt.config.js
package.json

nuxt.config.js

module.exports =  {
  head: {
    titleTemplate: '%s',
    title: 'Project',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  css: [
    '@/assets/css/main.scss'
  ],

  plugins: [
  ],

  components: true,

  buildModules: [
    '@nuxtjs/vuetify'
  ],

  modules: [
    'nuxt-socket-io',
    'nuxt-i18n',
    '@nuxtjs/axios',
    '@nuxtjs/auth-next'
  ],

  io: {
    sockets: [
      {
        name: 'main',
        url: process.env.APP_SERVER_URL,
        default: true
      }
    ]
  },

  i18n: {
    locales: [
      {
        code: 'en',
        file: 'en-US.js'
      }
    ],
    lazy: true,
    langDir: 'lang/',
    defaultLocale: 'en'
  },

  serverMiddleware: [
    { path: '/api', handler: '~/api/index.js' },
  ],

  axios: {
    baseURL: process.env.APP_SERVER_URL,
  },

  vuetify: {
    customVariables: ['~/assets/variables.scss'],
    theme: {
      dark: true,
      themes: {
        dark: {},
        light: {}
      }
    }
  },

  build: {
    extend(config) {}
  }
}

package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "nodemon -w server -w nuxt.config.js server",
    "build": "nuxt generate",
    "start": "cross-env NODE_ENV=production node server",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@nuxtjs/auth-next": "5.0.0-1611574754.9020f2a",
    "@nuxtjs/axios": "^5.12.5",
    "body-parser": "^1.19.0",
    "core-js": "^3.8.3",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "http": "0.0.1-security",
    "moment": "^2.29.1",
    "nuxt": "^2.14.12",
    "nuxt-i18n": "^6.18.0",
    "nuxt-socket-io": "^1.1.14"
  },
  "devDependencies": {
    "@nuxtjs/vuetify": "^1.11.3",
    "cross-env": "^7.0.3",
    "nodemon": "^2.0.7"
  }
}

server/index.js

require('dotenv').config();

const isProd = process.env.NODE_ENV === 'production'
const http = require('http')
const app = require('express')()
const server = http.createServer(app)
const io = require('socket.io')(server)
const axios = require('axios')

const { Nuxt, Builder } = require('nuxt')
const config = require('../nuxt.config.js');
config.dev = !isProd;

const nuxt = new Nuxt(config)
const { host, port } = nuxt.options.server
if (config.dev) {
    const builder = new Builder(nuxt)
    builder.build()
} else {
    nuxt.ready()
}
app.use(nuxt.render)

server.listen(port, () => {
    console.log(`Server listening on http://${host}:${port}`)
});

// other logic

我需要一个可以安装在其他计算机上的 exe 来运行 Nodejs 服务器和 Nuxt 东西,就像我通过 npm run devnpm run build/start< 运行代码一样 在本地的开发计算机中。

我已经通过运行 nexe -i server 尝试了 nexe 但没有成功。我还有其他方法吗?

谢谢。

最佳答案

我想你可以看看pm2 .你可以用它运行 Node 服务器和其他东西。

关于node.js - 有什么办法可以从 Nuxt+Node 项目中生成可执行文件 (.exe) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66066951/

相关文章:

javascript - 是否可以从 Vorpal 提示符重新启动 Vorpal 应用程序?

node.js - 采用 RESTful 架构的长轮询(用户通知)

javascript - Nuxt js 客户端特定布局加载

使用 Cloud Functions 的 iOS 后台通知

javascript - 异步/等待未分配的对象

javascript - Apollo 不可分配给 'VueClass<Vue> 类型的参数

error-handling - Nuxt 处理来自 Prismic API 的获取错误

security - 在客户端将Json Web token JWT存储在哪里最安全?

javascript - Nuxt + Vuex - 如何将 Vuex 模块分解为单独的文件?

node.js - 找不到快速命令