0.0.2 • Published 2 years ago
vite-plugin-dev-server-api v0.0.2
vite-plugin-dev-server-api
Configure custom api(action and retrun) for vite dev server
Install
pnpm add vite-plugin-dev-server-api -D
Use
// vite.config.ts
import devServerApi from "vite-plugin-dev-server-api";
export default {
// ...
plugins: [
// ...,
devServerApi(options?: {}),
],
// ...
};
// api/index.ts
import fs from "fs/promise";
export default {
"/dev/server/api/json/data": async function () {
// You can call all the node.js capabilities here
let ctn = await fs.readFile("../json/data.json", { encoding: "utf-8" });
return ctn;
},
};
axios.get("/dev/server/api/json/data").then((res) => {
console.log(res); // json/data.json content
});
Features
- Auto import api form
options.dir
(default:api
). - Support
.js|.cjs|.mjs|.ts|json
file inoptions.dir
. (with default export, eg:export default/module.exports
) - Full TypeScript support.
- duplicate addresses prompt