45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
const express = require('express');
|
|
const app = express();
|
|
|
|
// Simulate our auth middleware (mounted at /api like the real one)
|
|
app.use('/api', (req, res, next) => {
|
|
console.log('[auth] req.url:', req.url, 'req.path:', req.path);
|
|
next();
|
|
});
|
|
|
|
// Our rewrite middleware (no mount path)
|
|
app.use((req, res, next) => {
|
|
console.log('[rewrite_before] req.url:', req.url, 'req.path:', req.path, 'req.originalUrl:', req.originalUrl);
|
|
if (req.url.startsWith('/api/sre')) {
|
|
req.url = '/api' + req.url.slice(9);
|
|
console.log('[rewrite_after] req.url:', req.url, 'req.path:', req.path);
|
|
}
|
|
next();
|
|
});
|
|
|
|
// Route handler
|
|
app.get('/api/info', (req, res) => {
|
|
console.log('[handler] req.url:', req.url, 'req.path:', req.path);
|
|
res.json({ ok: true, url: req.url, path: req.path });
|
|
});
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
console.log('[404] req.url:', req.url, 'req.path:', req.path);
|
|
res.status(404).json({ error: 'not found' });
|
|
});
|
|
|
|
app.listen(6001, () => {
|
|
console.log('Listening on 6001');
|
|
// Make a test request to ourselves
|
|
const http = require('http');
|
|
http.get('http://127.0.0.1:6001/api/sre/info', (res) => {
|
|
let data = '';
|
|
res.on('data', c => data += c);
|
|
res.on('end', () => {
|
|
console.log('Response:', data);
|
|
process.exit(0);
|
|
});
|
|
});
|
|
});
|