How do I access nginx header variables in meteor - meteor

In my nginx config I've got proxy_pass set to my meteor app. I'm also setting some header variables using proxy_set_header. How do I get access to these variables in my meteor app?
In my main.js on the server I've tried a ton of things in Meteor.onConnection(), but haven't been able to see the values of the variables set by nginx.
Thanks.

You might be able to get the values from the headers for http requests with Webapp.connectHandlers or Webapp.rawConnectHandlers
// server/main.js
Meteor.startup(() => {
WebApp.rawConnectHandlers.use(function(req, res, next) {
console.log(req.headers);
next();
});
}

Related

What path should I use for Meteor's Webapp API?

I'm using Meteor v1.9 Webapp API to have my app listen to HTTP requests, specifically from a link to the app itself from a website, let's say example.org.
The documentation says to use
WebApp.connectHandlers.use([path], handler)
Where the [path] is defined as such:
path - an optional path field. This handler will only be called on
paths that match this string. The match has to border on a / or a ..
For example, /hello will match /hello/world and /hello.world, but not
/hello_world.
My question:
Let's say my meteor application is hosted on abc.com and the POST data being sent over to it is from example.org (where the link to abc.com is as well).
For the [path] argument mentioned above, in this case, should I have it as "/example" since example.org is where my app is listening to requests from (getting the POST data)? Or does it have to be a different format? I tried the former, but it doesn't seem to be working for it, so I'm trying to find the root of the issue.
Additional information that might be useful: I know it says 'optional' so I tried omitting it, but when I tested it out via 'meteor run' where it runs off of localhost:3000, it just yielded a blank page, with no errors and a success sent back, probably because it uses a GET request instead of POST.
My code for the webapp in my meteor application is as follows:
WebApp.connectHandlers.use("/[example]", async (req, res, next) => {
userName = req.body;
res.writeHead(200);
res.end();
});
Also technically my meteor application is built/bundled and deployed as a Node.js application on the website, but that shouldn't affect anything regarding this as far as I could tell.
That path is the path (part of the URL) on your meteor server. So in your example, for instance,
WebApp.connectHandlers.use("/example", async (req, res, next) => {
userName = req.body;
res.writeHead(200);
res.end();
});
means that you will need to send your POST requests to abc.com/example.

firebase cloud function redirect with express

I'm trying to automatically determine which language of the website should be user redirected to.
Setup is Firebase cloud function with ExpressJS server and Angular Universal SSR.
When preferred language is determined from request.acceptsLanguages(...) I'm trying to redirect via response.redirect('/en');.
When debugging it locally via firebase serve it redirects, but when deployed it doesn't seem to work at all, even logs from that endpoint don't show in the log list.
// All base routes are redirected to language specific
app.get('/', (req, res) => {
console.log('this is /');
if (req.acceptsLanguages('cs', 'cs-CZ', 'sk', 'sk-CZ')) {
res.redirect(`/cs`);
} else {
res.redirect(`/en`);
}
});
// All regular routes use the Universal engine
app.get('*', (req, res) => {
console.log('this is *');
res.render('index', { req });
});
export const ssr = functions.https.onRequest(app);
The problem was in the index.html being served as a static file on the / route.
I resolved it by renaming the Angular's index.html to web-index.html (any descriptive name will do).
It could also be solved by configuring the Express static file server to not serve index.html files if found for the matching routes, but that would disable serving any pages that have been pre-rendered (if you have SSR prerendering setup).

CORS error when use cors middleware in an api built with node.js and Express

I am new to node and express. I have encountered a cors error when I am building a very simple API. I have tried several hours to solve it in different method but none of these work.
Here's my approach
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({ origin: true }));
app.get('/api', (req, res) => {
res.send('Hello');
});
exports.api = functions.https.onRequest(app);
and got 4 errors all about :
http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
I have also tried several other some methods like this:
var allowCrossDomain = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cache-Control");
next();
};
app.use(allowCrossDomain);
Which gives me the same error.
I am using Firebase Cloud Function to deploy this api, because the code is so simple so I really can not figure out which part is not doing right.
CORS is always a sticky situation, but in this case, I think I might be able to help. When you run firebase deploy you should see your endpoint get deployed. If it's the first time you are deploying that function, it should print out that new function's full URL in the console, it usually looks something like this:
https://us-central1-your-project-name.cloudfunctions.net/apiEndpointName
If you've already deployed it, you can see the function's full URL in the Firebase console -> Functions -> Dashboard
That URL is the normal public API endpoint or "HTTP-trigger" for that function. If you would use Postman to make a GET request to that URL, you should expect to receive your Hello response. (Or if you visited that URL in your browser, your browser would make a GET request to that URL, you should get your Hello response there too)
The problem comes when you want to access it from your deployed/hosted website. You need to tell the hosting portion of Firebase to route any traffic for /api to your function - your Firebase hosting should not try to resolve the /api route as a normal HTML page deployed along-side the primary index.html file... instead it should direct any traffic for /api to the cloud function api
So, you need to tell Firebase to direct any traffic for /api to the cloud function, not hosting. You give Firebase commands/configuration in the firebase.json file... in this case, under a section named "rewrites" like this:
{
"hosting": {
"public": "public",
// Add the following rewrites section *within* "hosting"
"rewrites": [ {
"source": "/bigben", "function": "bigben"
} ]
}
}
Check out this documentation link where it explains all that^^
Once you've done that, redeploy everything, and now you should be able to visit /api in your browser and trigger the function. NOTE Unless you are using firebase serve you should visit the route on the deployed website, not localhost. Check out this link for more details on firebase serve.

Http, get response from server with rxjs and angular2

I don't know what I am doing wrong, I don't get any response from the server to work using Http. I added the project as plunker if someone could have a deeper look.
https://plnkr.co/edit/3HybQY3vcsCImbuFCWrP?p=preview
post.service.ts
getPosts(){
return this._http.get("http://jsonplaceholder.typicode.com/posts")
.map(res => res.json());
}
app.component.ts
constructor(private _postService: PostService) {
_postService.getPosts()
.subscribe(res => console.log(res));
}
You are making cross-domain request
You need to add some staff to http request header:
Access-Control-Allow-Origin: *
More details: How to create cross-domain request (Angular 2)?
Wasn't anything wrong, worked when restarted my lite server.
There are two major mistakes in your code that's your code is not running as expected.
you are using Http request response but you have not configure #angular/http setting in your config file so you have to add this in the file.
'#angular/http': {
main: 'bundles/http.umd.js',
defaultExtension: 'js'
},
You are trying to get request over http which angular2 won't allow so you have to change your url like
http://jsonplaceholder.typicode.com/posts
to
https://jsonplaceholder.typicode.com/posts
here is your working code demo
Working Example

Meteor Modify Boilerplate Response with Iron Router

Using Iron Router I can add a route such as /index returns "INDEX CONTENT" from the server:
this.route('index', {
path: '/',
where: 'server',
action: function () {
this.response.end("INDEX CONTENT");
}
});
The default behaviour for a Meteor app is to return a boilerplate HTML file on the initial request to the server which contains the js/css etc required to run the web app.
What I would like to do, however, is place a string (ie "INDEX CONTENT" as above) within the boilerplate which would normally be returned by default if I hadn't added the route. To do this, I'd need to be able to modify a boilerplate response before it is sent to the client but after it is constructed by the standard meteor response mechanism.
Can anyone recommend a way to be able to do this?
You could try the inject-initial meteorite package.
From the docs:
Inject.rawModHtml(id, func). At injection time, calls func(html, res) with the full page HTML which it expects to be returned, in full, after modification. res is the current http connection response request.
I think you would use it like this.
Inject.rawModHtml('breakEverything', function(html) {
return "INDEX CONTENT";
});

Resources