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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-spa-server

v1.0.0

Published

Vite plugin Single-Port between Backend and Frontend

Readme

Vite SPA Server

Vite plugin Single-Port between Backend and Frontend.

Design for Single Fighter Developer who want to build Fullstack Application with ease.

ci npm version License download-url

Features

  • Single-Port for Backend and Frontend.
  • Sharing types between Backend and Frontend.
  • Supports backend frameworks (Express, Hono, NHttp, etc.).
  • Hot Module Replacement (HMR) for Frontend during development.
  • Easy integration with existing Vite projects (React, Vue, Svelte, etc.).
  • Simple configuration.
  • Multiple apps using config area.

Why use Vite SPA Server?

When developing a Single Page Application (SPA) with a backend API, managing separate ports for the frontend and backend can be cumbersome. Vite SPA Server simplifies this by allowing both to run on a single port, streamlining development and reducing configuration overhead.

Usage

Create vite project.

Example: React as frontend and Express as backend (both typescript).

npm create vite@latest my-app -- --template react-ts

cd my-app

npm install

then, install vite-spa-server and express

npm install express

npm install vite-spa-server @types/express --save-dev

In your vite.config.ts file, import and use the plugin:

import { defineConfig } from "vite";
import spaServer from "vite-spa-server";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [
    react(),
    spaServer({
      entry: "./src/server.ts",
      port: 3000,
      serverType: "express",
    }),
  ],
});

then, create minimal server at ./src/server.ts:

import express from "express";

const app = express();

// you can call this route in the frontend with same port like fetch("/api/user").
app.get("/api/user", (req, res) => {
  res.json({
    name: "John",
    message: "Hello from the backend!",
  });
});

export default app;

now, you can run your Vite development server:

npm run dev

build for production:

npm run build

run production server:

node dist

Configuration Options

  • port: The port number on which the server will run.
  • serverType: The type of backend server to use (e.g., express, hono, nhttp, etc.).
  • entry: The entry point file for your backend server (default: ./src/server.(ts|js)).
  • runtime: The runtime environment for the server (default: node).
  • build: Build options for the server.
  • area: Map path to html for multiple apps.
  • routerType: Router Type between Backend and Frontend (e.g., browser, hash, none).
  • startServer: When true, the server will start immediately after initialization. when false, just export your app.

Handle 404 for API

app.use((req, res, next) => {
  if (req.url.startsWith("/api")) {
    res.header("spa-server", "false").status(404);
    return res.send({ error: "API endpoint not found" });
  }
  next();
});

Multiple Apps using area

export default defineConfig({
  plugins: [
    react(),
    spaServer({
      port: 3000,
      serverType: "express",
      area: {
        "/": "./index.html",
        "/admin": "./admin.html",
        "/sign": "./sign.html",
        // more...
      },
    }),
  ],
});

License

MIT