@edgeone/nuxt-pages 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -205,7 +205,7 @@ export default async function handler(req, res, context) {
|
|
|
205
205
|
});
|
|
206
206
|
|
|
207
207
|
return new Response(response.body, {
|
|
208
|
-
status: response.statusCode,
|
|
208
|
+
status: response.status || response.statusCode,
|
|
209
209
|
headers: response.headers
|
|
210
210
|
});
|
|
211
211
|
} catch (nitroError) {
|
package/package.json
CHANGED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import { resolve, dirname } from 'path';
|
|
2
|
-
import { fileURLToPath } from 'url';
|
|
3
|
-
import { readFileSync, existsSync, statSync } from 'fs';
|
|
4
|
-
import { extname } from 'path';
|
|
5
|
-
|
|
6
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
-
const __dirname = dirname(__filename);
|
|
8
|
-
|
|
9
|
-
// Static assets directory
|
|
10
|
-
const ASSET_DIR = resolve(__dirname, '../assets');
|
|
11
|
-
|
|
12
|
-
// MIME type mapping
|
|
13
|
-
const MIME_TYPES = {
|
|
14
|
-
'.html': 'text/html; charset=utf-8',
|
|
15
|
-
'.js': 'application/javascript',
|
|
16
|
-
'.css': 'text/css',
|
|
17
|
-
'.json': 'application/json',
|
|
18
|
-
'.png': 'image/png',
|
|
19
|
-
'.jpg': 'image/jpeg',
|
|
20
|
-
'.jpeg': 'image/jpeg',
|
|
21
|
-
'.gif': 'image/gif',
|
|
22
|
-
'.svg': 'image/svg+xml',
|
|
23
|
-
'.ico': 'image/x-icon',
|
|
24
|
-
'.txt': 'text/plain',
|
|
25
|
-
'.xml': 'application/xml'
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Get the MIME type of a file
|
|
30
|
-
*/
|
|
31
|
-
function getMimeType(filePath) {
|
|
32
|
-
const ext = extname(filePath).toLowerCase();
|
|
33
|
-
return MIME_TYPES[ext] || 'application/octet-stream';
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Handle HTTP response
|
|
38
|
-
*/
|
|
39
|
-
async function handleResponse(response, req, res, context) {
|
|
40
|
-
if (!response) {
|
|
41
|
-
res.statusCode = 500;
|
|
42
|
-
res.setHeader('Content-Type', 'text/plain');
|
|
43
|
-
res.end('Server Error');
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Ensure response.headers is a Headers object
|
|
48
|
-
if (!(response.headers instanceof Headers)) {
|
|
49
|
-
response.headers = new Headers(response.headers || {});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Correctly iterate over Headers object (using entries() method)
|
|
53
|
-
for (const [key, value] of response.headers.entries()) {
|
|
54
|
-
res.setHeader(key, value);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Check if Content-Type already exists (case-insensitive)
|
|
58
|
-
const hasContentType = response.headers.has('content-type');
|
|
59
|
-
|
|
60
|
-
// Only set default value if Content-Type is missing
|
|
61
|
-
if (!hasContentType) {
|
|
62
|
-
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
63
|
-
}
|
|
64
|
-
// try {
|
|
65
|
-
// res.setHeader('functions-request-id', context.headers['x-scf-request-id']);
|
|
66
|
-
// } catch (error) {
|
|
67
|
-
// console.error('Handle response error:', error);
|
|
68
|
-
// }
|
|
69
|
-
|
|
70
|
-
res.setHeader('from-server', 'true');
|
|
71
|
-
|
|
72
|
-
// Handle set-cookie header (special handling, as there may be multiple values)
|
|
73
|
-
if (response.headers.has('set-cookie')) {
|
|
74
|
-
const cookieArr = response.headers.getSetCookie();
|
|
75
|
-
res.setHeader('set-cookie', Array.isArray(cookieArr) ? cookieArr : [cookieArr]);
|
|
76
|
-
// headers['set-cookie'] = Array.isArray(cookieArr) ? cookieArr : [cookieArr];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
res.statusCode = response.status || response.statusCode || 200;
|
|
80
|
-
if(Buffer.isBuffer(response.body)) {
|
|
81
|
-
res.end(response.body);
|
|
82
|
-
} else if(response.body && typeof response.body === 'object' && typeof response.body.getReader === 'function') {
|
|
83
|
-
const reader = response.body.getReader();
|
|
84
|
-
const chunks = [];
|
|
85
|
-
while (true) {
|
|
86
|
-
const { done, value } = await reader.read();
|
|
87
|
-
if (done) break;
|
|
88
|
-
chunks.push(Buffer.from(value));
|
|
89
|
-
}
|
|
90
|
-
res.end(Buffer.concat(chunks));
|
|
91
|
-
} else {
|
|
92
|
-
res.end(response.body || response._data || '');
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Lazy load Nitro application
|
|
98
|
-
*/
|
|
99
|
-
let nitroApp = null;
|
|
100
|
-
async function getNitroApp() {
|
|
101
|
-
if (!nitroApp) {
|
|
102
|
-
// Set environment variables to prevent automatic server startup
|
|
103
|
-
process.env.NITRO_PORT = '';
|
|
104
|
-
process.env.PORT = '';
|
|
105
|
-
|
|
106
|
-
// Set correct static assets path
|
|
107
|
-
process.env.NITRO_PUBLIC_DIR = ASSET_DIR;
|
|
108
|
-
|
|
109
|
-
const { {{USE_NITRO_APP_SYMBOL}}: useNitroApp } = await import('./chunks/nitro/nitro.mjs');
|
|
110
|
-
nitroApp = useNitroApp();
|
|
111
|
-
}
|
|
112
|
-
return nitroApp;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Handle HTTP response
|
|
117
|
-
*/
|
|
118
|
-
function handleResponse(response, event) {
|
|
119
|
-
if (!response) {
|
|
120
|
-
return {
|
|
121
|
-
statusCode: 500,
|
|
122
|
-
headers: { 'Content-Type': 'text/plain' },
|
|
123
|
-
body: 'Internal Server Error'
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const headers = {};
|
|
128
|
-
|
|
129
|
-
// Ensure response.headers is a Headers object
|
|
130
|
-
if (!(response.headers instanceof Headers)) {
|
|
131
|
-
response.headers = new Headers(response.headers || {});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Correctly iterate over Headers object (using entries() method)
|
|
135
|
-
for (const [key, value] of response.headers.entries()) {
|
|
136
|
-
headers[key] = value;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Check if Content-Type already exists (case-insensitive)
|
|
140
|
-
const hasContentType = response.headers.has('content-type');
|
|
141
|
-
|
|
142
|
-
// Only set default value if Content-Type is missing
|
|
143
|
-
if (!hasContentType) {
|
|
144
|
-
headers['Content-Type'] = 'text/html; charset=utf-8';
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
headers['from-server'] = 'true';
|
|
148
|
-
headers['functions-request-id'] = event.headers['x-scf-request-id'];
|
|
149
|
-
|
|
150
|
-
// Handle set-cookie header (special handling, as there may be multiple values)
|
|
151
|
-
if (response.headers.has('set-cookie')) {
|
|
152
|
-
const cookieArr = response.headers.getSetCookie();
|
|
153
|
-
headers['set-cookie'] = Array.isArray(cookieArr) ? cookieArr : [cookieArr];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
statusCode: response.status || response.statusCode || 200,
|
|
158
|
-
headers,
|
|
159
|
-
body: response.body || response._data || ''
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* EdgeOne function handler
|
|
165
|
-
*/
|
|
166
|
-
export async function handler(event) {
|
|
167
|
-
try {
|
|
168
|
-
const url = event.path || '/';
|
|
169
|
-
const method = event.httpMethod || event.method || 'GET';
|
|
170
|
-
const headers = event.headers || {};
|
|
171
|
-
const body = event.body || '';
|
|
172
|
-
|
|
173
|
-
// First try to handle static assets
|
|
174
|
-
if (method === 'GET') {
|
|
175
|
-
const staticResponse = handleStaticFile(url);
|
|
176
|
-
if (staticResponse) {
|
|
177
|
-
return staticResponse;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Handle dynamic requests
|
|
182
|
-
const app = await getNitroApp();
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
const response = await app.localCall({
|
|
186
|
-
url,
|
|
187
|
-
method,
|
|
188
|
-
headers,
|
|
189
|
-
body
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
return handleResponse(response, event);
|
|
193
|
-
} catch (nitroError) {
|
|
194
|
-
// Handle Nitro static file read errors (prerender files not found)
|
|
195
|
-
// Check error and its cause property (H3Error may wrap actual error in cause)
|
|
196
|
-
const actualError = nitroError?.cause || nitroError;
|
|
197
|
-
const errorPath = actualError?.path || nitroError?.path;
|
|
198
|
-
const errorCode = actualError?.code || nitroError?.code;
|
|
199
|
-
|
|
200
|
-
// If error is due to prerender static file not found, try dynamic rendering
|
|
201
|
-
if (errorCode === 'ENOENT' &&
|
|
202
|
-
errorPath &&
|
|
203
|
-
(errorPath.includes('/assets/') || errorPath.includes('assets/')) &&
|
|
204
|
-
(errorPath.includes('/index.html') || errorPath.includes('index.html'))) {
|
|
205
|
-
console.warn(`Prerender file not found: ${errorPath}, falling back to dynamic rendering for ${url}`);
|
|
206
|
-
|
|
207
|
-
// If static file handling has been tried but file not found, should perform dynamic rendering
|
|
208
|
-
// Nitro should be able to handle dynamic routes, but if it still tries to read static files,
|
|
209
|
-
// it may be due to configuration issues. We throw an error directly to let user know to build or check configuration
|
|
210
|
-
throw new Error(`Prerender route ${url} not found. Make sure to run build first or configure routeRules correctly. Original error: ${actualError?.message || nitroError?.message}`);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Other errors are thrown directly
|
|
214
|
-
throw nitroError;
|
|
215
|
-
}
|
|
216
|
-
} catch (error) {
|
|
217
|
-
console.error('EdgeOne handler error:', error);
|
|
218
|
-
return {
|
|
219
|
-
statusCode: 500,
|
|
220
|
-
headers: { 'Content-Type': 'text/plain' },
|
|
221
|
-
body: `Internal Server Error: ${error.message}`
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
import('http').then(async (http) => {
|
|
227
|
-
const { createServer } = http;
|
|
228
|
-
// Dynamically import stream module to handle ReadableStream
|
|
229
|
-
await import('stream').then(({ Readable, pipeline }) => {
|
|
230
|
-
const server = createServer(async (req, res) => {
|
|
231
|
-
try {
|
|
232
|
-
const event = {
|
|
233
|
-
path: req.url,
|
|
234
|
-
httpMethod: req.method,
|
|
235
|
-
headers: req.headers,
|
|
236
|
-
body: ''
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
240
|
-
const chunks = [];
|
|
241
|
-
for await (const chunk of req) {
|
|
242
|
-
chunks.push(chunk);
|
|
243
|
-
}
|
|
244
|
-
event.body = Buffer.concat(chunks).toString();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const result = await handler(event);
|
|
248
|
-
|
|
249
|
-
res.statusCode = result.statusCode;
|
|
250
|
-
// Object.entries(result.headers).forEach(([key, value]) => {
|
|
251
|
-
// if(key === 'set-cookie') {
|
|
252
|
-
res.setHeader('set-cookie', Array.isArray(value) ? value[0].split(',') : value);
|
|
253
|
-
// } else {
|
|
254
|
-
// res.setHeader(key, value);
|
|
255
|
-
// }
|
|
256
|
-
// });
|
|
257
|
-
// debug message
|
|
258
|
-
// res.setHeader('functions-request-id', req.headers['x-scf-request-id']);
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
// Handle response body: support Buffer, string, and ReadableStream
|
|
262
|
-
if (Buffer.isBuffer(result.body)) {
|
|
263
|
-
res.end(result.body);
|
|
264
|
-
} else if (result.body && typeof result.body === 'object' && typeof result.body.getReader === 'function') {
|
|
265
|
-
// Detect ReadableStream (Web Streams API)
|
|
266
|
-
try {
|
|
267
|
-
const nodeStream = Readable.fromWeb(result.body);
|
|
268
|
-
nodeStream.pipe(res);
|
|
269
|
-
} catch (streamError) {
|
|
270
|
-
console.error('Stream conversion error:', streamError);
|
|
271
|
-
// If conversion fails, try to read the entire stream
|
|
272
|
-
const reader = result.body.getReader();
|
|
273
|
-
const chunks = [];
|
|
274
|
-
try {
|
|
275
|
-
while (true) {
|
|
276
|
-
const { done, value } = await reader.read();
|
|
277
|
-
if (done) break;
|
|
278
|
-
chunks.push(Buffer.from(value));
|
|
279
|
-
}
|
|
280
|
-
res.end(Buffer.concat(chunks));
|
|
281
|
-
} catch (readError) {
|
|
282
|
-
console.error('Stream read error:', readError);
|
|
283
|
-
res.end();
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
} else {
|
|
287
|
-
// Handle string or other types
|
|
288
|
-
res.end(result.body || '');
|
|
289
|
-
}
|
|
290
|
-
} catch (error) {
|
|
291
|
-
console.error('Local server error:', error);
|
|
292
|
-
res.statusCode = 500;
|
|
293
|
-
res.setHeader('Content-Type', 'text/plain');
|
|
294
|
-
res.end(`Server Error: ${error.message}`);
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
const port = process.env.DEV_PORT || 9000;
|
|
299
|
-
server.listen(port, () => {
|
|
300
|
-
console.log(`EdgeOne development server running at http://localhost:${port}`);
|
|
301
|
-
console.log(`Static assets served from: ${ASSET_DIR}`);
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
// import { createServer } from 'http';
|
|
2
|
-
// import {
|
|
3
|
-
// createRequestContext,
|
|
4
|
-
// runWithRequestContext,
|
|
5
|
-
// } from './.edgeone/dist/run/handlers/request-context.cjs';
|
|
6
|
-
// import {
|
|
7
|
-
// createEdgeOneNuxtServerHandler,
|
|
8
|
-
// defaultEdgeOneNuxtConfig
|
|
9
|
-
// } from './.edgeone/dist/run/handlers/nuxt-server.cjs';
|
|
10
|
-
// import {
|
|
11
|
-
// createEdgeOneNuxtCacheHandler
|
|
12
|
-
// } from './.edgeone/dist/run/handlers/nuxt-cache.cjs';
|
|
13
|
-
|
|
14
|
-
// // Initialize Nuxt server handler
|
|
15
|
-
// const nuxtServerHandler = createEdgeOneNuxtServerHandler(defaultEdgeOneNuxtConfig);
|
|
16
|
-
|
|
17
|
-
// async function handleResponse(res, response, passHeaders = {}) {
|
|
18
|
-
// const startTime = Date.now();
|
|
19
|
-
|
|
20
|
-
// if (!response) {
|
|
21
|
-
// res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
22
|
-
// res.end(JSON.stringify({
|
|
23
|
-
// error: "Not Found",
|
|
24
|
-
// message: "The requested path does not exist"
|
|
25
|
-
// }));
|
|
26
|
-
// console.log(`HandleResponse: 404 Not Found - ${Date.now() - startTime}ms`);
|
|
27
|
-
// return;
|
|
28
|
-
// }
|
|
29
|
-
|
|
30
|
-
// try {
|
|
31
|
-
// if (response instanceof Response) {
|
|
32
|
-
// const headers = Object.fromEntries(response.headers);
|
|
33
|
-
// Object.assign(headers, passHeaders);
|
|
34
|
-
|
|
35
|
-
// // Handle set-cookie headers properly
|
|
36
|
-
// if (response.headers.has('set-cookie')) {
|
|
37
|
-
// const cookieArr = response.headers.getSetCookie();
|
|
38
|
-
// headers['set-cookie'] = cookieArr;
|
|
39
|
-
// }
|
|
40
|
-
|
|
41
|
-
// res.writeHead(response.status, headers);
|
|
42
|
-
|
|
43
|
-
// if (response.body) {
|
|
44
|
-
// const arrayBuffer = await response.arrayBuffer();
|
|
45
|
-
// res.end(Buffer.from(arrayBuffer));
|
|
46
|
-
// } else {
|
|
47
|
-
// res.end();
|
|
48
|
-
// }
|
|
49
|
-
// } else {
|
|
50
|
-
// // Handle non-Response objects
|
|
51
|
-
// res.writeHead(200, { 'Content-Type': 'application/json', ...passHeaders });
|
|
52
|
-
// res.end(JSON.stringify(response));
|
|
53
|
-
// }
|
|
54
|
-
// } catch (error) {
|
|
55
|
-
// console.error('Error in handleResponse:', error);
|
|
56
|
-
// res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
57
|
-
// res.end(JSON.stringify({
|
|
58
|
-
// error: "Internal Server Error",
|
|
59
|
-
// message: error.message
|
|
60
|
-
// }));
|
|
61
|
-
// } finally {
|
|
62
|
-
// console.log(`HandleResponse: ${response?.status || 'unknown'} - ${Date.now() - startTime}ms`);
|
|
63
|
-
// }
|
|
64
|
-
// }
|
|
65
|
-
|
|
66
|
-
// // Enable regional blobs for EdgeOne
|
|
67
|
-
// process.env.USE_REGIONAL_BLOBS = 'true';
|
|
68
|
-
|
|
69
|
-
// export default async function handler(req, context) {
|
|
70
|
-
// const requestContext = createRequestContext(req, context);
|
|
71
|
-
|
|
72
|
-
// // Initialize cache handler for this request
|
|
73
|
-
// const cacheHandler = createEdgeOneNuxtCacheHandler({
|
|
74
|
-
// maxMemoryCacheSize: 50 * 1024 * 1024, // 50MB
|
|
75
|
-
// revalidateOnStale: true
|
|
76
|
-
// });
|
|
77
|
-
|
|
78
|
-
// // Add cache handler to request context
|
|
79
|
-
// requestContext.cacheHandler = cacheHandler;
|
|
80
|
-
|
|
81
|
-
// const handlerResponse = await runWithRequestContext(requestContext, async () => {
|
|
82
|
-
// try {
|
|
83
|
-
// // Convert Node.js request to Web API Request
|
|
84
|
-
// const url = new URL(req.url, `http://${req.headers.host}`);
|
|
85
|
-
// const method = req.method || 'GET';
|
|
86
|
-
|
|
87
|
-
// let body = null;
|
|
88
|
-
// if (method !== 'GET' && method !== 'HEAD') {
|
|
89
|
-
// body = createReadableStreamFromRequest(req);
|
|
90
|
-
// }
|
|
91
|
-
|
|
92
|
-
// const webRequest = new Request(url.toString(), {
|
|
93
|
-
// method,
|
|
94
|
-
// headers: req.headers,
|
|
95
|
-
// body
|
|
96
|
-
// });
|
|
97
|
-
|
|
98
|
-
// // Handle request with Nuxt server handler
|
|
99
|
-
// return await nuxtServerHandler.handleRequest(webRequest, requestContext);
|
|
100
|
-
// } catch (error) {
|
|
101
|
-
// console.error('Error in Nuxt handler:', error);
|
|
102
|
-
|
|
103
|
-
// return new Response(JSON.stringify({
|
|
104
|
-
// error: 'Internal Server Error',
|
|
105
|
-
// message: error.message,
|
|
106
|
-
// timestamp: new Date().toISOString()
|
|
107
|
-
// }), {
|
|
108
|
-
// status: 500,
|
|
109
|
-
// headers: {
|
|
110
|
-
// 'Content-Type': 'application/json',
|
|
111
|
-
// 'X-Handled-By': 'EdgeOne-Nuxt-Error'
|
|
112
|
-
// }
|
|
113
|
-
// });
|
|
114
|
-
// }
|
|
115
|
-
// });
|
|
116
|
-
|
|
117
|
-
// return handlerResponse;
|
|
118
|
-
// }
|
|
119
|
-
|
|
120
|
-
// export const config = {
|
|
121
|
-
// path: '/*',
|
|
122
|
-
// preferStatic: true,
|
|
123
|
-
// };
|
|
124
|
-
|
|
125
|
-
// // Development server configuration
|
|
126
|
-
// const port = 9000;
|
|
127
|
-
|
|
128
|
-
// // Convert Node.js request stream to Web API ReadableStream
|
|
129
|
-
// function createReadableStreamFromRequest(req) {
|
|
130
|
-
// return new ReadableStream({
|
|
131
|
-
// start(controller) {
|
|
132
|
-
// req.on('data', chunk => {
|
|
133
|
-
// // Convert Buffer to Uint8Array
|
|
134
|
-
// const uint8Array = new Uint8Array(chunk);
|
|
135
|
-
// controller.enqueue(uint8Array);
|
|
136
|
-
// });
|
|
137
|
-
|
|
138
|
-
// req.on('end', () => {
|
|
139
|
-
// controller.close();
|
|
140
|
-
// });
|
|
141
|
-
|
|
142
|
-
// req.on('error', error => {
|
|
143
|
-
// controller.error(error);
|
|
144
|
-
// });
|
|
145
|
-
// },
|
|
146
|
-
|
|
147
|
-
// cancel() {
|
|
148
|
-
// // Clean up resources
|
|
149
|
-
// req.destroy();
|
|
150
|
-
// }
|
|
151
|
-
// });
|
|
152
|
-
// }
|
|
153
|
-
|
|
154
|
-
// // Development server for local testing
|
|
155
|
-
// // Start development server directly when running the script
|
|
156
|
-
// const server = createServer(async (req, res) => {
|
|
157
|
-
// try {
|
|
158
|
-
// const response = await handler(req, { waitUntil: () => {} });
|
|
159
|
-
// await handleResponse(res, response);
|
|
160
|
-
// } catch (error) {
|
|
161
|
-
// console.error('Development server error:', error);
|
|
162
|
-
// res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
163
|
-
// res.end(JSON.stringify({
|
|
164
|
-
// error: 'Development Server Error',
|
|
165
|
-
// message: error.message
|
|
166
|
-
// }));
|
|
167
|
-
// }
|
|
168
|
-
// });
|
|
169
|
-
|
|
170
|
-
// server.listen(port, () => {
|
|
171
|
-
// console.log(`Nuxt development server running on http://localhost:${port}`);
|
|
172
|
-
// });
|
|
173
|
-
|
|
174
|
-
// Ensure Nitro can read import.meta and env like in built index
|
|
175
|
-
globalThis._importMeta_ = { url: import.meta.url, env: process.env };
|
|
176
|
-
|
|
177
|
-
// Choose port
|
|
178
|
-
process.env.NITRO_PORT = process.env.NITRO_PORT || '9000';
|
|
179
|
-
process.env.PORT = process.env.PORT || process.env.NITRO_PORT;
|
|
180
|
-
|
|
181
|
-
// Import and execute Nitro server (top-level code starts the server)
|
|
182
|
-
const nitroModule = await import('./chunks/nitro/nitro.mjs');
|
|
183
|
-
|
|
184
|
-
// Get useNitroApp (your build exports it as `k`)
|
|
185
|
-
const useNitroApp = nitroModule.k || nitroModule.useNitroApp;
|
|
186
|
-
if (!useNitroApp) {
|
|
187
|
-
console.error('useNitroApp not found in nitro module exports');
|
|
188
|
-
process.exit(1);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const nitroApp = useNitroApp();
|
|
192
|
-
|
|
193
|
-
// Request interceptor
|
|
194
|
-
nitroApp.hooks.hook('request', async (event) => {
|
|
195
|
-
const { req } = event.node;
|
|
196
|
-
console.log(`📥 [${new Date().toISOString()}] ${req.method} ${req.url}`);
|
|
197
|
-
console.log('📋 Request Headers:', req.headers);
|
|
198
|
-
// Example: mark request source
|
|
199
|
-
req.headers['x-intercepted-by'] = 'custom-hooks';
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
// Response interceptor
|
|
203
|
-
nitroApp.hooks.hook('beforeResponse', async (event, response) => {
|
|
204
|
-
const { res } = event.node;
|
|
205
|
-
console.log(`📤 [${new Date().toISOString()}] Status: ${res.statusCode}`);
|
|
206
|
-
console.log('📋 Response Headers:', res.getHeaders());
|
|
207
|
-
// Example: add a custom header
|
|
208
|
-
res.setHeader('x-intercepted', 'true');
|
|
209
|
-
return response;
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
console.log('🔍 Request/Response interception enabled (run-with-hooks)');
|