connect-modrewrite to rewrite url - node.js

I am running webrtc html on node.js port 8080
I am trying to rewrite the url on node.js
I installed both modules connect-url-rewrite and connect-modrewrite
can you advice me with example and where can add the script on server.js or on html pages itself
Please advice

You don't need both connect-url-rewrite and connect-modrewrite. Pick one of them and go with that.
I'd suggest reading up a little on how Connect middleware works.
Essentially, you create a Connect app, and call use on it, passing in the rewrite function with a set of rules. There's a good example the Github page for connect-modrewrite. I've reposted it here with some simplifications:
var app = connect()
app.use(modRewrite([
'^/test$ /index.html',
'^/test/\\d*$ /index.html [L]',
'^/test/\\d*/\\d*$ /flag.html [L]'
]))
app.use(connect.static(PATH_TO_STATIC_FILES_DIR))
app.listen(3000)
If you don't know what the strings inside of the modRewrite call mean, you'll have to read up a little on rewrite rules. Essentially, the pattern is: <regular expression for url fragment> <target> <optional flags>
Hope that helps!

Related

Oauth error invalid_request: The redirect_uri is not whitelisted

I'm trying to develop an app with React and Node based on this documentation:
I followed the tutorial step by step but I'm stuck in testing the app with this URL format:
I replaced the ngrok address and my shopify store but I get the 404 error (This page could not be found.)
I found the same question in this link. So I renamed the .env to process.env but I still have the same problem.
Here is the .env file:
package.json :
server.js :
The error :
I don't think my answer is going to help many , But i am going to put it here anyway. So i had the same issue recently and i tried everything in stack-overflow and shopify community. And finally the problem was i had mistakenly copied my another app's apikey. So even if the apiKey is wrong the error probably you are going to face is the same "The redirect uri is not whitelisted."
Mistake I was doing that I forgot to postpend /auth/callback to Forwarding URL to the Allowed redirection URL(s) section generated by following command:
ngrok http 3000
so redirect url will be like:
https://SOME_STRING.ngrok.io/auth/callback
Yes there is an issue with your callback URL you have to define your app URL and callback URL in your shopify partner account where you create shopify app
Shopify partner account
You have to do as follow
open apps >> yourapp >> app setup >> Insert In URLs(Whitelisted redirection URL(s))
Once you whitelist your URL there then the issue is solve
Your Whitelisted redirection URL must be https
Make sure that App URL and Redirection URL in App Setup is correct
something like -
App URL => https://example.com/
Redirection URL => https://example.com/auth/callback (make sure don't put "/" in the last)
and in your project HOST should be: https://example.com/
this will surely help you!!!
I accidentally used the http address instead of the https one.
I guess that will happen once you restart your ngrok, and your ngrok address url has changed, you have to manually update your new ngrok in the following part:
Your AppUrl and Redirection Url in App setup. (Shopify dashboard)
In your .env file, Where you defined your SHOPIFY_APP_URL. (Code Editor)
I had a '/' at the end of SHOPIFY_APP_URL in the .env file, so the redirect url was wrong with '//auth/callback'
The problem I was facing was that the SHOPIFY_API_KEY and SHOPIFY_API_SECRET in the .env file were in quotes which are the same in your case too. For this quote error also, it was showing the same error. Basically it is the wrong API keys error. Can you try removing quotes and try again?
If that doesn't work, please check you are using HTTPS URL and not HTTP
I stuck in this error for 5-6 hours and it was just because I used ngrok and the https in ngrok is not working very well...
so I upload all of my content to a real server with https subdomain and everything is working fine now
Don't forget to check these for the host variable in your env file:
There should be no trailing / on the URL.
there should be no protocol mentioned for the URL
HOST=abcxyz.com and not HOST:https://abcxyz.com/
[ Solved ] 2022-08-03
I ran into the same error when I was trying to install Shopify App on Shopify Development Store for testing.
Make sure that there are no trailing slashes when copying and pasting the ngrok url in .env file
I was getting this error because I used a trailing slash - https://<Your ngrok url>/
. Using it without trailing slash https://<Your ngrok url> fixed the issue.
Also, make sure that you include the trailing slash in your App Settings in Shopify Admin. Even if you don't include it, Shopify will save it with a trailing slash automatically.
Next, test installing your app. If you see any other error then make sure that you are using proper Shopify API scopes in your .env file.
For me, it was that, in my beginAuth method, I had the whole url as the callback. It just wants the path.
I took it from
let authRoute = await Shopify.Auth.beginAuth(
req,
res,
process.env.SHOPIFY_STORE,
'https://stuff.ngrok.io/auth/shopify/callback',
false
)
to
let authRoute = await Shopify.Auth.beginAuth(
req,
res,
process.env.SHOPIFY_STORE,
'/auth/shopify/callback',
false
)
For me it was because I accidentally commented out the code where I set accessToken and shop
This error occurs when you use HTTP instead of HTTPS in allow redirection links secession in Shopify.

Always redirect http traffic to https traffic in google app engine nodejs flex environment [duplicate]

I've followed the answer of this: Redirect from http to https in google cloud but it does not seem to be currently accurate any more. The anchor referenced ( https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml#security ) seems to have been removed but without a note of a replacement.
For reference, I am serving NodeJS over a Google App (flex) Engine. As per the answer I've got in my app.yaml:
handlers:
- url: /.*
script: IGNORED
secure: always
Since HTTPS is obviously terminated before it hits my Express engine (and redirection on there would be useless); how is it currently correctly implemented?
Potentially helpful, I have an external domain attached via the "Custom domains" tab in the console, and there is indeed a SSL certificate configured (so if a user manually goes to https://.com everything is fine)
The flexible environment does not current support handlers in the app.yaml. If you want https:// redirection, you have a few options:
Use helmet to do to HSTS stuff for you, and implement your own initial redirect.
I wrote a happy little library to always forces SSL on all routes for express yes-https
We are considering auto-redirecting all traffic to SSL by default. Do you think that would be a good thing for your apps?
Pulling Justin's yes-https library, I was able to get this to work:
var app = express();
app.use(function(req, res, next){
if (req.host != 'localhost' && req.get('X-Forwarded-Proto') == 'http') {
res.redirect(`https://${req.host}${req.url}`);
return;
}
app.router(req, res, next);
});
At first I thought I had to do that since I was on an appengine subdomain and couldn't use HSTS. Then I learned HSTS works fine for subdomains. :) Regardless, I thought people might want to see what the magic bit to use was if they didn't want to use yes-https for some reason.
Justin, auto-redirecting all traffic to SSL by default sounds great to me. I just spent hours trying to figure out how to do so before I found this post because I was trying to get my app to get Chrome's add to homescreen install banner as per https://developers.google.com/web/fundamentals/engage-and-retain/app-install-banners/.
GCP This should be as easy to just use the gcloud app cli and configure a header (Strict-Transport-Security) or redirect rule. Perhaps the push is to force us to Firebase Hosting instead which is forcing HTTPS already. For a quick solution for Single Page apps (static content) with React, Angular etc, we can use this JS snippet.
It ignores localhost environments. You can change localhost with a host name that you would like to exclude. It then redirects using https as protocol.
if ( location.host.indexOf("localhost") < 0 && location.protocol.toLowerCase() !== "https:"){
const url= `https://${location.host}`;
location.replace(url);
}

HTTP to HTTPS redirection on App Engine Flexible

I've followed the answer of this: Redirect from http to https in google cloud but it does not seem to be currently accurate any more. The anchor referenced ( https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml#security ) seems to have been removed but without a note of a replacement.
For reference, I am serving NodeJS over a Google App (flex) Engine. As per the answer I've got in my app.yaml:
handlers:
- url: /.*
script: IGNORED
secure: always
Since HTTPS is obviously terminated before it hits my Express engine (and redirection on there would be useless); how is it currently correctly implemented?
Potentially helpful, I have an external domain attached via the "Custom domains" tab in the console, and there is indeed a SSL certificate configured (so if a user manually goes to https://.com everything is fine)
The flexible environment does not current support handlers in the app.yaml. If you want https:// redirection, you have a few options:
Use helmet to do to HSTS stuff for you, and implement your own initial redirect.
I wrote a happy little library to always forces SSL on all routes for express yes-https
We are considering auto-redirecting all traffic to SSL by default. Do you think that would be a good thing for your apps?
Pulling Justin's yes-https library, I was able to get this to work:
var app = express();
app.use(function(req, res, next){
if (req.host != 'localhost' && req.get('X-Forwarded-Proto') == 'http') {
res.redirect(`https://${req.host}${req.url}`);
return;
}
app.router(req, res, next);
});
At first I thought I had to do that since I was on an appengine subdomain and couldn't use HSTS. Then I learned HSTS works fine for subdomains. :) Regardless, I thought people might want to see what the magic bit to use was if they didn't want to use yes-https for some reason.
Justin, auto-redirecting all traffic to SSL by default sounds great to me. I just spent hours trying to figure out how to do so before I found this post because I was trying to get my app to get Chrome's add to homescreen install banner as per https://developers.google.com/web/fundamentals/engage-and-retain/app-install-banners/.
GCP This should be as easy to just use the gcloud app cli and configure a header (Strict-Transport-Security) or redirect rule. Perhaps the push is to force us to Firebase Hosting instead which is forcing HTTPS already. For a quick solution for Single Page apps (static content) with React, Angular etc, we can use this JS snippet.
It ignores localhost environments. You can change localhost with a host name that you would like to exclude. It then redirects using https as protocol.
if ( location.host.indexOf("localhost") < 0 && location.protocol.toLowerCase() !== "https:"){
const url= `https://${location.host}`;
location.replace(url);
}

Node.js routing with Apache proxy

I'm running Apache on a Linux server and find that my node.js apps routing doesn't work correctly when using URLS that go through Apache.
I followed instructions on other posts for turning on the proxy modules within apache and then set up this definition:
ProxyPass /testauth http://localhost:3000/
(I also had a line in there to define a similar reverse proxy that did nothing)
My node app runs on port 3000 with routing set up like in app.js
app.use('/', routes);
app.use('/tests', tests);
app.use('/questions', questions);
If I access URLs on my server like this:
http://rose.cs.umass.edu:3000/
http://rose.cs.umass.edu:3000/questions/179
I get correct results (note the styling with some simple CSS)
If I try to access thru apache like this:
http://rose.cs.umass.edu/testauth/questions/179
I get a 404 error but see from the backtrace that it is inside my js code.
For some reason the URL http://rose.cs.umass.edu/testauth partially works (but CSS doesn't get applied (because the index.ejs file includes the css like this:
<link rel='stylesheet' href='/stylesheets/style.css' />
The location of CSS files and the coding of relative paths follows all the node.js examples I have looked at and works fine in my development environment. But when running the app by going thru Apache, things are not working right.
Is there a fix to the apache config? Do I have to make an adjustment to how code paths in the my javascript and ejs files?
One clue I had to what was wrong is that node was printing the paths each time a request came in and they looked like
GET //questions/ 404 4.278 ms - 916
So I changed apache to have:
ProxyPass /testauth http://localhost:3000 (removed trailing slash)
Now the paths coming into my app were correctly formed.
The next order of business is to rework all the relative URLs in my EJS templates, client-side javascript, and even redirects in the server side.
Because I had to put "testauth" in the path so that Apache can forward to node, all the URLs coming into the app are of the form rose.cs.umass.edu/testauth/rest-of-url . This means relative URLs inside my app code that looked like
<a href="/questions" ....>
were now incorrect because they created absolute URLs that omitted the testauth in the path. Some of these relative URLs now are pretty strange (using things like ../ or even ../.. to get to things) . I wish there was a variable similar to ${pageContext.request.contextPath} that I use in JSP to build URLs.
I decided to mimic the JSP ${pageContext} idea
I added to file called config.js that has various settings that are particular to where the app is running (stuff like db credentials, etc). I added
// uncomment the line that applies
config.pageContext = "" // dev environment
// config.pageContext = "testauth" // production environment
My routing scripts all do something like:
res.render('questions', {pageContext: util.pageContext(req), questions: questionArray});
And the EJS templates all have stuff like this in the URLS:
<script src="<%= pageContext %>/static/js/bootstrap.min.js"></script>
A second alternative that I used for awhile until I went with the above:
Pass an argument to your node app when you start it. You then set a global variable similar to above and use it similarly. If you use a process manager (I use pm2) to run your node app on your server you might have to do something like:
pm2 start testauth -- -c /testauth // start testauth.bin using /testauth for the page context

All requests via Proxy in Node-Webkit

Is there a way to force Node-webkit to use proxied settings for all requests ? I know there is an API solution using setProxyConfig() but this doesn't work with authenticated proxy, the prompt shows up to login but crash when submitted...
So I tried to use node request module, and it works fine :
var request=require('request');
var proxy = request.defaults({'proxy':'http://login:pwd#proxy:port'});
But now my problem is to tell node-webkit to use this for each request.
Any solutions ?
I'm quite new using node and node-webkit so maybe there is a better way to do that.
Thanks a lot !
You may try
process.env.http_proxy = 'http://login:pwd#proxy:port'
It will work with request lib, also should impact other requests from node-webkit (assets, ajax)
But may be other libraries don't use environment proxy settings. If that doesn't work you can try https://www.npmjs.com/package/global-tunnel
Not sure if this is solved, but i came up with a simple solution:
i wrote a simple wrapper for the request library
https://gist.github.com/jodevsa/7dd9662b8244359fa0d7626ae7c9bd69
all you have to do is ,
Head to $PROJECT_DIR/node_modules/request/
Rename index.js to core.js
Create a new file called index.js
Copy code content from the above gist link to index.js
Change proxyLoc value to you'r preferred proxy
If you decided to disable proxy , just change value of ON variable to false inside index.js
Cheers :D

Resources