Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@astrojs/node

Package Overview
Dependencies
Maintainers
4
Versions
111
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@astrojs/node

Deploy your site to a Node.js server

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
59K
increased by4.81%
Maintainers
4
Weekly downloads
 
Created
Source

@astrojs/node 🔲

This adapter allows Astro to deploy your SSR site to Node targets.

  • Why Astro Node
  • Installation
  • Usage
  • Configuration
  • Examples
  • Troubleshooting
  • Contributing
  • Changelog

Why Astro Node

If you're using Astro as a static site builder—its behavior out of the box—you don't need an adapter.

If you wish to use server-side rendering (SSR), Astro requires an adapter that matches your deployment runtime.

Node is a JavaScript runtime for server-side code. Frameworks like Express are built on top of it and make it easier to write server applications in Node. This adapter provides access to Node's API and creates a script to run your Astro project that can be utilized in Node applications.

Installation

First, install the @astrojs/node package using your package manager. If you're using npm or aren't sure, run this in the terminal:

npm install @astrojs/node

Then, install this adapter in your astro.config.* file using the adapter property:

astro.config.mjs

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

export default defineConfig({
  // ...
  adapter: node()
})

Usage

After performing a build there will be a dist/server/entry.mjs module that exposes a handler function. This works like a middleware function: it can handle incoming requests and respond accordingly.

Using a middleware framework

You can use this handler with any framework that supports the Node request and response objects.

For example, with Express:

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 output script does not require you use Express and can work with even the built-in http and https node modules. The handler does follow the convention calling an error 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);

Configuration

This adapter does not expose any configuration options.

Examples

Troubleshooting

Contributing

This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!

Changelog

FAQs

Package last updated on 30 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc