繁体   English   中英

如何通过 express JS 或 Plesk 在子路径上部署 Nuxt

[英]how to deploy Nuxt on sub path by express JS or Plesk

我已经通过 Plesk 运行了 Nuxt(节点 js 应用程序)。 这意味着 Plesk 将运行使它运行 ExpressJS 的 server.js 文件,而 ExpressJS 将运行 Nuxt。 我的 server.js 文件如下:

const express = require('express')
const consola = require('consola')
const { Nuxt } = require('nuxt')
const app = express()
const port = process.env.PORT;

const config = require('./nuxt.config.js')
config.dev = process.env.NODE_ENV !== 'production'


async function start() {
  const nuxt = new Nuxt(config)
  const { host } = nuxt.options.server
  const port = process.env.PORT;
  await nuxt.ready()
  app.use(nuxt.render)
  app.listen(port, host)
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true,
  })
}
start()

我需要在 sample.com/blog 上为 Nuxt 提供服务,并在 sample.com 上为 home/inxe.html 提供服务,如何通过 express js 或 Plesk 做到这一点? 我更喜欢使用 ExpressJs,但我不知道该怎么做,请帮忙

我试图通过如下更改 server.js 来解决我的问题,但它没有用

const express = require('express')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')
const app = express()

const config = require('./nuxt.config.js')
config.dev = process.env.NODE_ENV !== 'production'

async function start() {
  const nuxt = new Nuxt(config)
  const { host } = nuxt.options.server
  const port = process.env.PORT;
  await nuxt.ready()
  app.use(nuxt.render)
  app.listen(port, host)
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true,
  })
}
 app.get('/blog',(req,res)=>{
 start()
 })
 app.get('/',(req,res)=>{
   res.send('Hello World!')
 })

 app.listen(port, ()=>{
   console.log(`Example app listening on port ${port}`)
 })

sample.com 抛出 hello world 但 sample.com/blog 抛出错误

我通过在服务器中间件中重定向解决了我的问题
要点:为静态站点提供服务,我们需要 serve-static 包
你应该在 nuxt.confgi.js 中配置 serverMiddleware 属性

 serverMiddleware: [
    { path: '/', handler: serveStatic(__dirname + '/staticSites/homePage') },
    { path: '/landing', handler: serveStatic(__dirname + '/staticSites/privacy') },
    { path: '/landing/Privacy.html', handler: serveStatic(__dirname + '/staticSites/privacy',{ index: ['Privacy.html'] }) },
    { path: "/app.apk", handler: "~/server-middleware/download.js" },
    { path: "/calorie.mp4", handler: "~/server-middleware/video_calorie.js" },
    { path: "/Zireh7.2.1-Sibapp.ipa", handler: "~/server-middleware/Zireh7.2.1-Sibapp.js" },
    '~/server-middleware/redirect.js'
  ],

要直接下载文件,我们需要在 download.js 中添加以下代码

const path = require('node:path');
const app = require('express')()

    app.all('/', (req, res) => {
      const file = `${path.resolve()}/directDownload/app.apk`;
      res.download(file); // Set disposition and send it.
    })
    module.exports = app

暂无
暂无

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

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