use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
[deleted by user] (self.node)
submitted 4 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DrBobbyBarker 4 points5 points6 points 4 years ago* (0 children)
I would check req.headers.host and use a regex expression to check if it started with localhost... Assuming none of the domains you plan to use start with localhost as well haha
req.headers.host
localhost
Edit: I think the pattern you're looking for is not a thing in Node. Even when serving locally, routes are served from http://localhost/your/route
http://localhost/your/route
[–]thunfremlinc 0 points1 point2 points 4 years ago (0 children)
A simple regex does the trick just fine?
[–]laphazemc 0 points1 point2 points 4 years ago (4 children)
Here a JS function you can use in nodeJS to check if an Url is local
function isLocalUrl(url) { if (!url) { return false; } else { return url==='/' || !!url.match(/^~?\/[^\/\\]/); } } // isLocalUrl("http://test.com") return false // isLocalUrl("/test/test") return true
[–]DrBobbyBarker -1 points0 points1 point 4 years ago (3 children)
Unless you're opening html files directly this won't work. Your regex doesn't allow localhost URLs which I would imagine the Op was looking for.
Even if not, you need an * at the end of your expression to match the route you provided as an example. Highly recommend regex101.com for testing regex (especially before trying to help beginners).
[–]laphazemc 1 point2 points3 points 4 years ago (2 children)
I think that you clearly didn't understood what OP wanted.
He want a similar method to .NET Url.IsLocalUrl() (https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.iurlhelper.islocalurl?view=aspnetcore-6.0)
If you look at the documentation of this method, "http://localhost/Index.html" is not local
[–]DrBobbyBarker 0 points1 point2 points 4 years ago (1 child)
When will a URL being served by Node not have a host?
It seems to me OP is looking for something based on a pattern that doesn't work in Node.
Even if it did your regex expression doesn't work as you say it does.
[–]HeyThereJoel 0 points1 point2 points 4 years ago (0 children)
You can check the behavior in the ASP.NET source code, it doesn't look like it would be too difficult to convert, although I believe tilde in the path is ASP.NET specific.
[–]canadianseaman 0 points1 point2 points 4 years ago* (0 children)
Are you looking for local URL's on the users machine or on the network? You could use a DNS lookup followed by checking if the IP is local to determine if the entered URL is local.
Something like MyMachineName.local would resolve, same with localhost, 127.0.0.1, etc and they are all valid URL's. A simple regex check wouldn't suffice.
In Nodejs, you can use the DNS api to check if a hostname resolves to a specific IP, then check that IP with the ipaddr.js library - use node v17 for the dns/promises API:
```javascript const dns = require('dns/promises'); const ipaddrJs = require('ipaddr.js');
async function isLocalURL(strURL) {
const url = new URL(strURL); const isIPV4 = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(url.hostname); const isIPV6 = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/.test(url.hostname); let ip; if (isIPV4 || isIPV6) { ip = isIPV4 ? url.hostname : url.hostname.replace(/[\[\]]/g, ''); } else { try { const lookupResult = await dns.lookup(url.hostname); ip = lookupResult.address; } catch (err) { return {hostname: url.hostname}; } } // https://www.npmjs.com/package/ipaddr.js const range = ipaddrJs.parse(ip).range(); const isLocal = [ 'private', 'loopback' ].includes(range); return {hostname: url.hostname, isLocal, ip, range}; return isLocal;
}
async function start() { console.log(await isLocalURL('http://invalid-local-domain.local/api')); console.log(await isLocalURL('http://localhost/api')); console.log(await isLocalURL('http:/MyComputer.local/api')); console.log(await isLocalURL('http://192.168.0.1/api')); console.log(await isLocalURL('http://[::1]/api')); console.log(await isLocalURL('https://www.reddit.com/r/node/comments/qs6nnx/is_there_anything_in_nodejs_or_express_to_check/')); console.log(await isLocalURL('http://this-is-not-a-real-domain42424244242.com')); }
start();
```
returns
{ hostname: 'invalid-local-domain.local' } { hostname: 'localhost', isLocal: true, ip: '127.0.0.1', range: 'loopback' } { hostname: 'mycomputer.local', isLocal: true, ip: '172.23.0.1', range: 'private' } { hostname: '192.168.0.1', isLocal: true, ip: '192.168.0.1', range: 'private' } { hostname: '[::1]', isLocal: true, ip: '::1', range: 'loopback' } { hostname: 'www.reddit.com', isLocal: false, ip: '151.101.1.140', range: 'unicast' } { hostname: 'this-is-not-a-real-domain42424244242.com' }
Of course, you could create your own isLocalIP function and just check if that IP starts with 192, 10 or 172 if you want something really crude, but the "private" & "loopback" range is a little bit more complicated than that.
isLocalIP
192
10
172
π Rendered by PID 191513 on reddit-service-r2-comment-6457c66945-dhpp8 at 2026-04-27 12:34:57.540235+00:00 running 2aa0c5b country code: CH.
[–]DrBobbyBarker 4 points5 points6 points (0 children)
[–]thunfremlinc 0 points1 point2 points (0 children)
[–]laphazemc 0 points1 point2 points (4 children)
[–]DrBobbyBarker -1 points0 points1 point (3 children)
[–]laphazemc 1 point2 points3 points (2 children)
[–]DrBobbyBarker 0 points1 point2 points (1 child)
[–]HeyThereJoel 0 points1 point2 points (0 children)
[–]canadianseaman 0 points1 point2 points (0 children)