npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vike-node

v0.2.9

Published

<!-- WARNING: keep links absolute in this file so they work on NPM too -->

Downloads

1,475

Readme

npm version

vike-node

[!WARNING] This package is in beta and will have relatively frequent breaking changes.

Server integration for Vike.

With this extension, your server is transpiled with Vite. So that you don't need ts-node/tsx anymore.

In development, the server process is restarted when a change is detected.

Installation
Custom pageContext
Standalone build
External packages
Compression
Version history

Installation

Overview
Add to existing server
Supported servers

Overview

Example of adding vike-node and Express.js to a Vike app that doesn't use a server yet.

[!NOTE]

  1. npm install vike-node express
  2. Extend vite.config.js:
    // vite.config.js
    
    import vikeNode from 'vike-node/plugin'
    
    export default {
      // ...
      plugins: [vikeNode('server/index.js')]
    }
  3. Create server/index.js:
    // server/index.js
    
    import express from 'express'
    import vike from 'vike-node/express'
    
    startServer()
    
    function startServer() {
      const app = express()
      app.use(vike())
      const port = 3000
      app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
    }
  4. Add production script:
      // package.json
    
      "scripts": {
        "dev": "vite",
        "build": "vite build",
    +   "prod": "NODE_ENV=production node dist/server/index.mjs"
      }

Add to existing server

If you already have a server:

// server/index.js

- import { renderPage } from 'vike/server'
+ import vike from 'vike-node/express'

- if (isProduction) {
-   app.use(express.static(`${root}/dist/client`))
- } else {
-   const vite = await import('vite')
-   const viteDevMiddleware = (
-     await vite.createServer({
-       root,
-       server: { middlewareMode: true }
-     })
-   ).middlewares
-   app.use(viteDevMiddleware)
- }

- app.get('*', async (req, res, next) => {
-   const pageContextInit = {
-     urlOriginal: req.originalUrl
-   }
-   const pageContext = await renderPage(pageContextInit)
-   const { httpResponse } = pageContext
-   if (!httpResponse) {
-     return next()
-   } else {
-     const { statusCode, headers } = httpResponse
-     headers.forEach(([name, value]) => res.setHeader(name, value))
-     res.status(statusCode)
-     httpResponse.pipe(res)
-   }
- })

+ app.use(vike())
  // package.json

  "scripts": {
    "build": "vite build",
-   "dev": "node ./server/index.js",
+   "dev": "vite",
-   "prod": "NODE_ENV=production node ./server/index.js"
+   "prod": "NODE_ENV=production node dist/server/index.mjs"
  }

Supported servers

vike-node includes middlewares for all commonly used server frameworks.

See complete list of supported servers.

Express

// server/index.js

import express from 'express'
import vike from 'vike-node/express'

startServer()

function startServer() {
  const app = express()
  app.use(vike())
  const port = 3000
  app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
}

Fastify

// server/index.js

import fastify from 'fastify'
import vike from 'vike-node/fastify'

startServer()

function startServer() {
  const app = fastify()
  app.all('/*', vike())
  const port = 3000
  app.listen({ port }, () => console.log(`Server running at http://localhost:${port}`))
}

Hono

// server/index.js

import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import vike from 'vike-node/hono'

startServer()

function startServer() {
  const app = new Hono()
  app.use(vike())
  const port = 3000
  serve(
    {
      fetch: app.fetch,
      port
    },
    () => console.log(`Server running at http://localhost:${port}`)
  )
}

H3

// server/index.js

import { createApp, toNodeListener } from 'h3'
import { createServer } from 'http'
import vike from 'vike-node/h3'

startServer()

async function startServer() {
  const app = createApp()
  app.use(vike())
  const port = 3000
  const server = createServer(toNodeListener(app)).listen(port)
  server.on('listening', () => {
    console.log(`Server running at http://localhost:${port}`)
  })
}

Elysia

// server/index.js

import { Elysia } from 'elysia'
import vike from 'vike-node/elysia'

startServer()

function startServer() {
  const app = new Elysia()
  app.get('/*', vike())
  const port = 3000
  app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
}

Custom pageContext:

You can define custom pageContext properties:

import { type RuntimeAdapter } from 'vike-node/express';

app.use(
  vike({
    pageContext(runtime: RuntimeAdapter) {
      return {
        user: runtime.req.user
      }
    }
  })
)

[!NOTE] See RuntimeAdapter (vike-node uses universal-middleware under the hood).

[!NOTE] The runtime object is also available at pageContext.runtime so that, even without the custom pageContext function above, you can retrieve pageContext.runtime.req.user in Vike hooks and UI components (with usePageContext()).

Standalone build

You can enable standalone builds by setting standalone to true. After build, the output dist folder will contain everything for a deployment. With standalone mode, the production environment only needs the dist folder to be present. Example start script: NODE_ENV=production node dist/server/index.mjs

// vite.config.js

import vikeNode from 'vike-node/plugin'

export default {
  // ...
  plugins: [
    vikeNode({
      entry: 'server/index.js',
      standalone: true
    })
  ]
}

External packages

Packages that import native binaries/custom assets need to be added to external. When building with standalone enabled, external packages and their assets are copied to the output dist directory. By default, the external setting includes:

  • sharp
  • @prisma/client
  • @node-rs/*
// vite.config.js

import vikeNode from 'vike-node/plugin'

export default {
  // ...
  plugins: [
    vikeNode({
      entry: 'server/index.js',
      standalone: true,
      external: ['my-rust-package']
    })
  ]
}

Compression

In production, vike-node compresses all Vike responses.

You can disable it:

app.use(
  vike({
    compress: false
  })
)