简体   繁体   中英

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

I have run Nuxt (node js application ) by Plesk. it means Plesk will run the server.js file that causes it to run ExpressJS and ExpressJS will run Nuxt. my server.js file is below:

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()

I need to serve Nuxt on sample.com/blog and serve home/inxe.html on sample.com how can I do this by express js or Plesk? I prefer to use ExpressJs but I do not know how do that please help

I have tried to solve my problem by changing server.js like below but it didn't work

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 throw hello world but sample.com/blog throw an error

I had solved my problem by redirecting in server middleware
point:to serve static sites we need serve-static package
you should config serverMiddleware property in nuxt.confgi.js

 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'
  ],

to download the file directly we need the below code in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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