I am tying to mock url's of the following form using the express module of node.js:
http://localhost:3002/example.domain.to.mock/features/location?lat=100.1234&lon=99.9876
But I can't seem to escape the question mark character when using a regex
var express = require('express');
var app = module.exports = express.createServer();
app.get('/example.domain.to.mock/features/location\?lat=*', function(req, res) {
sendMockResponseFromFile("mock_location.txt", res);
});
The above code does not work for url's with a question mark in them. I am able to mock requests with an ampersand in them, just not question marks. I have also tried '\\?' and '?' with no luck.
Express's router operates on only the path portion of the URL, not the entire URL, so routes can't match on the content of the query string. Your workaround is reasonable.
I found a workaround for my purposes: I simply accept all requests and then perform a simple regex test to send the appropriate mock file i need:
app.get('/example.domain.to.mock/features/location', function(req, res) {
var originalURL = req.originalUrl;
var regexPatternForFilteredSearch = new RegExp(".*myRequiredRegex.*");
if (regexPatternForFilteredSearch.test(originalURL)) {
sendMockResponseFromFile("mock_location.txt", res);
} else {
sendMockResponseFromFile("second_mock_location.txt", res);
}
});
I still am curious as to why I couldn't achieve using a regex inside the app.get() command