105 lines
2.3 KiB
JavaScript
105 lines
2.3 KiB
JavaScript
export default {
|
|
// 服务器端口
|
|
port: 8080,
|
|
|
|
// JavaScript中间件配置
|
|
middleware: {
|
|
// 请求前中间件 - 所有请求都会经过
|
|
onRequest: [
|
|
`
|
|
// 添加请求头
|
|
request.headers['X-Custom-Header'] = 'JavaScript-Middleware';
|
|
|
|
// 添加CORS头
|
|
request.headers['Access-Control-Allow-Origin'] = '*';
|
|
|
|
console.log('Processing request:', request.method, request.uri);
|
|
`
|
|
],
|
|
|
|
// 响应前中间件 - 返回给客户端之前
|
|
onResponse: [
|
|
`
|
|
// 修改响应头
|
|
response.headers['X-Response-Time'] = Date.now().toString();
|
|
|
|
console.log('Response status:', response.status);
|
|
|
|
// 返回响应继续处理
|
|
return response;
|
|
`
|
|
],
|
|
|
|
// 响应后中间件 - 发送到客户端之后
|
|
onResponseSent: [
|
|
`
|
|
console.log('Response sent to client');
|
|
`
|
|
]
|
|
},
|
|
|
|
// 站点配置
|
|
sites: {
|
|
// 示例站点1
|
|
'example.com': {
|
|
type: 'static',
|
|
path: '/var/www/example.com',
|
|
index: 'index.html',
|
|
|
|
// 站点级别的中间件
|
|
middleware: {
|
|
onRequest: [
|
|
`
|
|
// 只对example.com的请求添加特定头
|
|
request.headers['X-Site-Specific'] = 'example.com';
|
|
`
|
|
],
|
|
|
|
onResponse: [
|
|
`
|
|
// 修改特定站点的响应
|
|
response.headers['X-Powered-By'] = 'rhttpd with JavaScript';
|
|
`
|
|
]
|
|
},
|
|
|
|
routes: [
|
|
{
|
|
// 路由级别的中间件
|
|
path: '/api',
|
|
middleware: {
|
|
onRequest: [
|
|
`
|
|
// API特定的处理
|
|
if (request.uri.includes('/api/protected')) {
|
|
console.log('Protected API access');
|
|
}
|
|
`
|
|
]
|
|
}
|
|
}
|
|
]
|
|
},
|
|
|
|
// 示例站点2
|
|
'static-site.com': {
|
|
type: 'static',
|
|
path: '/var/www/static-site'
|
|
},
|
|
|
|
// 代理站点
|
|
'proxy-example.com': {
|
|
type: 'proxy',
|
|
target: 'http://localhost:3000',
|
|
|
|
middleware: {
|
|
onRequest: [
|
|
`
|
|
// 代理中间件
|
|
console.log('Proxying request to:', request.uri);
|
|
`
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}; |