Being new to WordPress, I've been doing some research and yet I don't seem to be able to pinpoint a solution for my need.
In short, I would like to allow a WordPress page to access a Node.js backend, the goal is ultimatly to get access to MongoDB via Node.js, retrieve some data and return a dynamically generated webpage to the website.
I was checking WordPress Rest API but all it seems to do is frontend handling of a WordPress website, creating and editing post, etc.
Unless there's a better way of doing it, I was thinking I might just send a get/post request from the WP page (like, with a form's action) and use Express.js to listen to that request, do the whole workflow on Node.js, then maybe use some npm wordpress API (like this one) to create a wordpress client and add a page or post with the DB extracted content.
I would appreciate some guidance, if any, as to how could one connect from WordPress to a Node.js backend.
Thanks a bunch!
There are a lot of ways to do it.
If you only need Node for a particular page then you can use your web server (NGINX/Apache) to reverse proxy a particular path to the Node server.
If you had to you could always use an HTML iframe as well but for some reason I feel like that's bad advise.
The method you described would work too. I was considering using GET/POST requests with Express running on a different port for a project I'm working on that uses Wordpress. I decided to go with the solution linked below.
This is probably the method you're looking for based on your description. Skip to solution three if you have to use Wordpress.
Node JS Reverse Proxy (with Apache)
You can find how to do it with NGINX with a quick search.
Related
I have existing app that runs on Heroku. It's a simple web app with no background jobs or database.
Basically it has three endpoints. One of them serves the HTML, the other is POST endpoint for communicating with backend and third is GET endpoint that renders error HTML content as well.
Now the frontend is not single page application and the goal is not to be one. It's just HTML page with a form and some links. The front end can be used without Javascript.
My questions are:
Can I re-use existing ExpressJS code? I was thinking about importing route callbacks and use them inside the "functions".
I know there is Firebase hosting that can serve static content. However, as I mentioned the server can respond with HTML content so I'd need traditional routing (such as /error url for rendering HTML error). In my ExpressJS app I use .ejs templating so I'd like to load the template and render it.
Can Firebase be set up so the "backend" (functions) can be placed on my own domain? I'd like to call relative URLs from my index file (like that POST endpoint) instead of using Google Firebase URL.
I'm really just trying to find out if my use case can fit the Firebase infrastructure. I think it's a good candidate since I need the web app to respond infrequently and it's not really demanding, not many people would use it. This is also my hobby project so I'm trying to minimize costs.
To answer your questions:
Yes you can, but keep in mind some things are limited. In my case, I had tried to use multer library for multipart forms but didn't work and had to resort to using busboy instead. To use an Express app for a function endpoint, you simply just pass your app as the onRequest function parameter like so: functions.https.onRequest(app).
Docs:
https://firebase.google.com/docs/functions/http-events#using_existing_express_apps
The hosting is for static files only. You can't use that with .ejs. Unless, you're thinking to use that as a proxy to your Express app..
Yes, you can set custom domains so you can use your domain(s) instead of default ones. Reference: https://firebase.google.com/docs/hosting/custom-domain
From your project's Hosting page, enter the wizard for connecting a custom domain:
If you have only one Hosting site, click Connect domain.
If you have more than one Hosting site, click View for the desired site, then click Connect domain.
Enter the custom domain name that you'd like to connect to your Hosting site.
(Optional) Check the box to redirect all requests on the custom domain
to a second specified domain (such that yourdomain.com and
www.yourdomain.com redirect to the same content).
Click Continue to initiate the validation process.
I have an already running website which frontend is made with React and a nodeJS/Express backend.
What I'm trying to accomplish now is to offer an RSS feed, but I don't know how to approach this.
I've found some ways to generate the RSS feed file (XML I suppose) in the backend, but then I'm not sure how to make it available in the frontend since it's a React one, in my server configuration (NGINX) every URL is redirected to the root and React takes care of routing. That's why I don't know how to make the RSS available in the frontend.
Also, I'm not sure what's the URL "convention" for serving RSS feed. I've seen some URLs with the protocol feed:// or ending in /rss too.
In the webpage we have some categories, so we'll have RSS for each of them, not just the root.
Any help on how to approach this problem will be more than welcome.
Thank you so much!
I finally got a way to do what I was looking for. This is what I've done, in case someone faces the same issues and can find this helpful.
I've added a new location in my NGINX configuration file:
location ~ ^[\/](?<tag>.*)[\/](feed) {
proxy_pass http://localhost:4000/feed/$tag;
}
The regex in that location is going to match any URL that contains /feed at the end of it and it's going to store in the variable tag the string of the "category" that I want to send to the backend to render the RSS feed.
So, for example, this will match: https://mywebpage.com/category/feed and it will store category in the variable tag.
With this done, just need to make the /feed endpoint available at the nodeJS/Express backend side.
I am trying to setup a node.js proxy server to allow me to send requests from firefox to the node.js server to view external websites
Generally i would like to set firefox settings to point to a working node proxy server to view external websites.
I need to see code for a working example the simpler the better
Look at this gist. It has some code samples
Look at the throttle-proxy npm package (throttling is optional).
I'm currently learning node.js and loving it. I noticing, however, that it seems that's it's really only fit for one site. So it's great for hosting mydomain.com, but what if I want to build an actual full web server with it. In other words, I would like to host mydomain.com, example.com, yourdomain.com and so on. What solutions (modules) are available for this? I was thinking of simply parsing the url from the request object and simply reading from the appropriate directory. For example if I get a request for example.com then read from the example_com directory or if I get a request from mydomain.com read from the mydomain_com directory. The issue here is I don't know how this will affect performance and scalability.
I've looked into Multi-node but I don't fully follow the idea of processes yet (I'm a node beginner).
Any suggestions are welcome.
You can do this a few different ways. One way is to write it directly into your web application by checking what domain the request was made to and then route within your application but unless your application is very basic this can make it fairly bloated and can get messy. A good time to do something like this might be if you're writing a blogging platform where everything is pretty much the same across all your domains. The key difference might be how you query your data to display the right data.
In this case you'd probably use the request to see which blog is being accessed.
If you want to just host a few different domains on the same server all using port 80 (like most websites do) you will want to proxy each request off to a different process. You can do this with nginx or even with node itself. It all comes down to what best fits your needs. bouncy is a quick way to get setup doing this as its a nodejs module and has some pretty impressive benchmarks. nginx (proxy with nginx) is probably the most wildly used method though, as a lot of nodejs servers use nginx to serve static content anyways.
http://blog.noort.be/2011/03/07/node-js-on-nginx.html
https://github.com/substack/bouncy/
You can use connect's vhost middleware (which is also available in express) to dispatch requests to separate request handlers based on the Host: header. This assumes that everything is being handled by the same node process on the same port; if you really need separate processes, then the suggestion about using nginx as a reverse proxy is probably the way to go.
I've got a CouchDB server up and running serving basic API requests. Overall it works well because users can GET/POST/PUT etc. to the host 'api.example.com'. The only issue is that if a user does a GET request for '/', they get the {"couchdb":"Welcome","version":"1.0.2"}.
Is there anyway to serve a single static HTML page or even an HTTP redirect for the root? That way I could redirect users to the API documentation.
I'm vaguely familiar with Couchapp but it seems like overkill for such a simple task.
Thanks!
You probably want a vhost/rewrite rule. They are pretty easy. Basically you tell CouchDB that queries to "www.example.com" should go directly to the rewriter. The rewriter will serve anything that you specify in the design document.
Jan Lehnardt wrote up good instructions in the Couchbase blog post, Nice URLs.