astro/packages/integrations/node
github-actions[bot] 8eec97fdd1
[ci] release (#3409)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-05-23 13:26:07 -04:00
..
src [ci] format 2022-04-14 13:52:56 +00:00
CHANGELOG.md [ci] release (#3409) 2022-05-23 13:26:07 -04:00
package.json [ci] release (#3409) 2022-05-23 13:26:07 -04:00
readme.md Update readme.md 2022-04-06 10:27:16 -05:00
tsconfig.json Adapters v0 (#2855) 2022-03-24 07:26:25 -04:00

@astrojs/node

An experimental server-side rendering adapter for use with Node.js servers.

In your astro.config.mjs use:

import { defineConfig } from 'astro/config';
import nodejs from '@astrojs/node';

export default defineConfig({
  adapter: nodejs()
})

After performing a build there will be a dist/server/entry.mjs module that works like a middleware function. You can use with any framework that supports the Node request and response objects. For example, with Express you can do:

import express from 'express';
import { handler as ssrHandler } from './dist/server/entry.mjs';

const app = express();
app.use(ssrHandler);

app.listen(8080);

Using http

This adapter does not require you use Express and can work with even the http and https modules. The adapter does following the Expression convention of calling a function when either

  • A route is not found for the request.
  • There was an error rendering.

You can use these to implement your own 404 behavior like so:

import http from 'http';
import { handler as ssrHandler } from './dist/server/entry.mjs';

http.createServer(function(req, res) {
  ssrHandler(req, res, err => {
    if(err) {
      res.writeHead(500);
      res.end(err.toString());
    } else {
      // Serve your static assets here maybe?
      // 404?
      res.writeHead(404);
      res.end();
    }
  });
}).listen(8080);