#how to run astro under a express server
1 messages · Page 1 of 1 (latest)
Hi @dusky wagon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To run Astro under an Express server, you need to configure Astro to run in middleware mode. This allows you to integrate Astro with your existing Express server.
In your Astro configuration file (astro.config.mjs), you need to set the mode option to 'middleware' under the adapter configuration. This will allow Astro to build to a middleware mode, which is the same behavior and API as before.
Here is an example of how to set this up:
import { defineConfig } from "astro/config";
import nodejs from "@astrojs/node";
export default defineConfig({
output: "server",
adapter: nodejs({ mode: "middleware" }),
});
This configuration tells Astro to output a server runtime and use the Node.js adapter in middleware mode.
For running Astro in development mode, you can use the command npm run dev, pnpm run dev, or yarn run dev in your terminal, depending on the package manager you are using.
npm run dev
or
pnpm run dev
or
yarn run dev
For production, you can start your server by running the script:
node ./dist/server/entry.mjs
Please note that you need to have the @astrojs/node adapter installed in your project. You can add it using the astro add command:
npx astro add @astrojs/node
or
pnpm astro add @astrojs/node
or
yarn astro add @astrojs/node
Sources:
- Astro 1.5.0 Release - Standalone mode in Node.js adapter (https://astro.build/blog/astro-150#standalone-mode-in-nodejs-adapter)
- Astro Documentation - Enable on-demand server rendering (https://docs.astro.build/en/guides/server-side-rendering#configure-server-or-hybrid)
- Astro Documentation - Add an Adapter (https://docs.astro.build/en/guides/server-side-rendering#add-an-adapter)