簡體   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