-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
39 lines (36 loc) · 1.05 KB
/
Copy pathvite.config.js
File metadata and controls
39 lines (36 loc) · 1.05 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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import fs from 'fs'
import path from 'path'
const DATA_FILE = path.resolve('./courses.json')
export default defineConfig({
plugins: [
react(),
{
name: 'courses-api',
configureServer(server) {
server.middlewares.use('/api/courses', (req, res) => {
res.setHeader('Content-Type', 'application/json')
res.setHeader('Access-Control-Allow-Origin', '*')
if (req.method === 'GET') {
if (fs.existsSync(DATA_FILE)) {
res.end(fs.readFileSync(DATA_FILE, 'utf-8'))
} else {
res.end('null')
}
} else if (req.method === 'POST') {
let body = ''
req.on('data', (chunk) => { body += chunk })
req.on('end', () => {
fs.writeFileSync(DATA_FILE, body, 'utf-8')
res.end('{"ok":true}')
})
} else {
res.statusCode = 405
res.end()
}
})
},
},
],
})