52 lines
1.0 KiB
JavaScript
52 lines
1.0 KiB
JavaScript
import { defineConfig } from 'vite';
|
|
import { returnOriginalJs } from '@kevisual/tojs';
|
|
import http from 'http';
|
|
const server = http.createServer(async (req, res) => {
|
|
console.log('req', req.url, req.method);
|
|
const method = req.method;
|
|
let body2 = {};
|
|
if (method === 'POST') {
|
|
const body = await parseBody(req);
|
|
console.log('body', body);
|
|
body2 = JSON.parse(body);
|
|
}
|
|
returnOriginalJs(
|
|
{
|
|
req,
|
|
res,
|
|
query: {
|
|
...body2,
|
|
},
|
|
},
|
|
{
|
|
prefixUrl: '/api/s1/js/',
|
|
},
|
|
);
|
|
});
|
|
server.listen(4005, () => {
|
|
console.log('server listen', server.address());
|
|
});
|
|
export const parseBody = async (req) => {
|
|
return new Promise((resolve, reject) => {
|
|
const arr = [];
|
|
req.on('data', (chunk) => {
|
|
arr.push(chunk);
|
|
});
|
|
req.on('end', () => {
|
|
resolve(Buffer.concat(arr).toString());
|
|
});
|
|
});
|
|
};
|
|
|
|
export default defineConfig({
|
|
server: {
|
|
port: 4003,
|
|
host: true,
|
|
proxy: {
|
|
'/api/s1/js': {
|
|
target: 'http://localhost:4005',
|
|
},
|
|
},
|
|
},
|
|
});
|