Nuxt的后端中的API Koa服务器未在Heroku(生产)上运行

问题描述 投票:0回答:1

我正在基于Koa template的通用模式下以Koa作为API /控制器运行Nuxt。我正在部署到Heroku。 API在本地运行良好,但在生产中返回404。我认为该应用程序在部署时会以SPA的形式运行,因为其他所有功能都可以正常运行。

这是我的服务器/index.js

const Koa = require('koa')
const consola = require('consola')
const Router = require('koa-router');
const { Nuxt, Builder } = require('nuxt')
const api = require('./api');

console.log('server works'); // ------> This line gets ignored by the Heroku console

const app = new Koa()
const router = new Router();

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = app.env !== 'production'

router.use('/api', api.routes(), api.allowedMethods());
app.use(router.routes());

async function start () {
  // Instantiate nuxt.js
  const nuxt = new Nuxt(config)

  const {
    host = process.env.HOST || '127.0.0.1',
    port = process.env.PORT || 3000
  } = nuxt.options.server

  // Build in development
  if (config.dev) {
    const builder = new Builder(nuxt)
    await builder.build()
  } else {
    await nuxt.ready()
  }

  app.use((ctx) => {
    ctx.status = 200
    ctx.respond = false // Bypass Koa's built-in response handling
    ctx.req.ctx = ctx // This might be useful later on, e.g. in nuxtServerInit or with nuxt-stash
    nuxt.render(ctx.req, ctx.res)
  })

  app.listen(port, host)
  consola.ready({
    message: `Server listening on http://${host}:${port}`, //  ------>  Neither this line appears in Heroku console
    badge: true 
  })
}

start()

Procfile

web: nuxt start

来自package.json的脚本

"scripts": {
    "dev": "cross-env HOST=192.168.1.65 NODE_ENV=development nodemon server/index.js --watch server ",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production node server/index.js",
    "generate": "nuxt generate",
    "test": "ava",
    "test:unit": "cross-env TEST=unit ava --config unit.config.js",
    "test:e2e": "cross-env TEST=e2e ava --config e2e.config.js",
    "heroku-postbuild": "nuxt build"
  }

我认为我在阅读all these部署文档后并没有看到明显的错误,但是却得到了nu(x)ts。

谢谢。

node.js heroku package.json koa nuxt
1个回答
0
投票

我没有任何问题,附加了package.jsonserver/index.js文件和Heroku环境设置。您可以从here

检查herokuapp

package.json

{
  "name": "testkoa",
  "version": "1.0.0",
  "description": "My first-class Nuxt.js project",
  "author": "Ahmet Zeybek",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",
    "build": "nuxt build",
    "start": "cross-env NODE_ENV=production node server/index.js",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@nuxtjs/axios": "^5.3.6",
    "@nuxtjs/dotenv": "^1.4.0",
    "@nuxtjs/pwa": "^3.0.0-0",
    "cross-env": "^5.2.0",
    "koa": "^2.6.2",
    "koa-router": "^7.4.0",
    "nuxt": "^2.0.0"
  },
  "devDependencies": {
    "nodemon": "^1.18.9"
  }
}

server / index.js

const Koa = require("koa");
const Router = require("koa-router");
const consola = require("consola");
const { Nuxt, Builder } = require("nuxt");
const app = new Koa();
// Import and Set Nuxt.js options
const config = require("../nuxt.config.js");
config.dev = app.env !== "production";

async function start() {
  app.use(async function handleError(ctx, next) {
    try {
      await next();
    } catch (err) {
      ctx.status = err.statusCode || err.status || 500;
      ctx.body = err;
    }
  });
  const router = new Router({ prefix: "/api" });
  router.get("/:name", async ctx => {
    ctx.response.body = `Hello ${ctx.params.name}`;
  });
  // Instantiate nuxt.js
  const nuxt = new Nuxt(config);

  const {
    host = process.env.HOST || "127.0.0.1",
    port = process.env.PORT || 3000
  } = nuxt.options.server;

  // Build in development
  if (config.dev) {
    const builder = new Builder(nuxt);
    await builder.build();
  } else {
    await nuxt.ready();
  }

  app.use(router.routes());
  app.use(router.allowedMethods());
  app.use(ctx => {
    ctx.status = 200;
    ctx.respond = false; // Bypass Koa's built-in response handling
    ctx.req.ctx = ctx; // This might be useful later on, e.g. in nuxtServerInit or with nuxt-stash
    nuxt.render(ctx.req, ctx.res);
  });

  app.listen(port, host);
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true
  });
}

start();

Heroku Config Vars

HOST=0.0.0.0
NODE_ENV=production
NPM_CONFIG_PRODUCTION=false
© www.soinside.com 2019 - 2024. All rights reserved.