-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
44 lines (42 loc) · 1.26 KB
/
vite.config.js
File metadata and controls
44 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
{
name: 'save-json-plugin',
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.method === 'POST' && req.url === '/api/save-data') {
let body = ''
req.on('data', chunk => {
body += chunk.toString()
})
req.on('end', () => {
try {
const { type, data } = JSON.parse(body)
const filePath = path.resolve(__dirname, `src/data/${type}.json`)
fs.writeFileSync(filePath, JSON.stringify(data, null, 2))
res.statusCode = 200
res.end(JSON.stringify({ success: true }))
} catch (error) {
res.statusCode = 500
res.end(JSON.stringify({ error: error.message }))
}
})
} else {
next()
}
})
}
}
],
server: {
allowedHosts: ['5974d3256e78.ngrok-free.app']
}
})