使用vite-express架設伺服器
使用vite開始專案的同時又想架設伺服器,可使用vite-express
https://github.com/szymmis/vite-express
安裝方式參照readme
//新專案
yarn create vite-express
//在現有專案下
yarn create vite
yarn add express vite-express
根目錄位置創造js檔
//e.g server.js
import express from "express";
import ViteExpress from "vite-express";
const app = express();
app.get("/message", (_, res) => res.send("Hello from express!"));
ViteExpress.listen(app, 3000, () => console.log("Server is listening..."));
node server.js
打開瀏覽器
vite.config設定(參考官方文件)
export default defineConfig({
plugins: [vue()],
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
}
}
}
})
Comments