UNPKG

vite-plugin-api-routes

Version:

A Vite.js plugin that creates API routes by mapping the directory structure, similar to Next.js API Routes. This plugin enhances the functionality for backend development using Vite.

35 lines (34 loc) 1.04 kB
// src/api-server/server.ts import { loadEnv } from "dotenv-local"; import express from "express"; import * as configure from "vite-plugin-api-routes/configure"; import { handler } from "vite-plugin-api-routes/handler"; var main = () => { const server = express(); configure.serverBefore?.(server); const { HOST, PORT } = loadEnv({ envPrefix: "SERVER_", removeEnvPrefix: true, envInitial: { SERVER_HOST: "127.0.0.1", SERVER_PORT: "3000" } }); const SERVER_URL = `http://${HOST}:${PORT}${API_ROUTES.BASE}`; server.use(API_ROUTES.BASE_API, handler); server.use(API_ROUTES.BASE, express.static(API_ROUTES.PUBLIC_DIR)); configure.serverAfter?.(server); const PORT_NRO = parseInt(PORT); server.listen(PORT_NRO, HOST, () => { console.log(`Ready at ${SERVER_URL}`); configure.serverListening?.(server, { HOST, PORT, SERVER_URL }); }).on("error", (error) => { console.error(`Error at ${SERVER_URL}`, error); configure.serverError?.(server, error); }); }; main();