Fixed vike-photon usage: PhotonRuntimeError Server entry default export must include a 'fetch' method

My press app is build with vike and vike-server. Recently vike team select new vike-photon and stop vike server development.

So I migrate vike-server to vike-photon. But finished code I catch the bellow runtime error when run dev:

PhotonRuntimeError: [photon][Runtime Error] Server entry default export must include a 'fetch' method

I double check the code and no find error. After try migrate the old code to the new blank vike-photon project code step by step. Finally I find the problem is my old code return Promise function of startApp in server entry:

// +config.ts
export { config }

import vikePhoton from 'vike-photon/config'

const config = {
  // https://vike.dev/extends
  extends: [vikePhoton],
  photon : {
    server: 'express-entry.ts',
    standalone: true
  }
}

// express-entry.ts
import "dotenv/config";
import { dbMiddleware } from "./db-middleware";
import { apply, serve } from "@photonjs/express";
import express from "express";

const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;

export default startApp() as unknown;

async function startApp() {
  const app = express();

  apply(app, [
    // Make database available in Context as `context.db`
    dbMiddleware,
    // Some handler,
  ]);

  // Other code used await.

  return serve(app, {
    port,
  });
}

After remove async and call function with no await. That's success running.

So I learned the usage of vike-photon key feature: NOT return Promise entry function.

50

Top articles