mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
13b271bc7d
* Bundle everything, commit 1 * Get everything working * Remove dependency on readable-stream * Adds a changeset * Fix ts errors * Use the node logger in tests * Callback the logger when done writing * Fix test helper to await the callback * Use serialize-javascript again * Remove dead code * Rename hook * Oops
1.3 KiB
1.3 KiB
@astrojs/node
An experimental server-side rendering adapter for use with Node.js servers.
In your astro.config.mjs use:
import nodejs from '@astrojs/node';
export default {
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);