I'm performing an http.request() to an app running locally, but it's hitting the wrong route, and I can't figure out why...
I believe the problem is with the subdomain not being acknowledged.
I'm currently using lvh.me:4000 for my development app. With the subdomain, the full url is api.lvh.me:4000
Here is what I have so far. The http.request() keeps hitting the /* route and returns html. What I want is for the app to hit the /subdomain/api/* route...
Server.js
if (process.env.NODE_ENV === 'development') {
app.use(require('express-subdomain-handler')({
baseUrl: 'lvh.me:4000',
logger: false
}));
}
Routes
// ------ Handle Missing External API Routes
app.get('/subdomain/api/*', function(req, res) {
res.status(404).json({
name: 'Missing',
message: 'Resource Not Found - Incorrect Route',
code: 'missing',
status: 404
});
});
// ------ Handle Missing Application Routes - Catch-All
app.get("/*", function(req, res) {
console.log(req)
res.status(404).render('404');
});
External request being performed from another app running locally
var options = {
hostname: api.lvh.me,
port: 4000,
path: '/missing_resource',
method: 'GET',
headers: headers
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
var body = '';
// Handle Errors
if (res.statusCode !== 200) {
// Aggregate Chunks
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
return callback(JSON.parse(body), null);
});
} else {
// Aggregate Chunks
res.on('data', function(chunk) {
body += chunk;
});
// Callback
res.on('end', function() {
return callback(null, JSON.parse(body));
});
}
});
req.on('error', function(e) {
console.log('Servant SDK Error', e);
});
req.end();
Again, the problem here is that the request is hitting /* instead of /subdomain/api/