CouchDB vhost rewrite to access root API - couchdb

I wish the following rewrite rule worked:
{
"from": "api/*",
"to": "../../../*"
}
… in a vhost rewrite like the following:
[vhosts]
myapp = /myapp/_design/myapp/_rewrite
Then it would be possible to access the root API in a following manner:
$.couch.urlPrefix = '/api';
var dbs = $.couch.allDbs({
success: function (data) {
console.log(data);
}
})
But unfortunately the request to http://myapp:5984/api/_all_dbs results into:
{"error":"insecure_rewrite_rule","reason":"too many ../.. segments"}
Am I missing something? Is something wrong with the rewrite? Does anyone know how to overcome that?
My Couchdb is 1.1.1.
I'm acquainted to this advice, but don't like any of the suggested ways.

Add
[httpd]
secure_rewrites=false
to your server's local.ini to disable this protection from cross-database rewrites.

Related

Hostinger, node.js, or express issue, routes not working anymore (except homepage / )

Firstly I'd like to thank you and everyone part of this community after years of lurking around I finally asked my first question, hopefully soon I'll be able to provide answers too! :)
So to summarize the problem, I'm getting 404's on my routes (except homepage) configuration is ubuntu20/nginx/nodejs/express app hosted on Hostinger VPS, I've tried hitting it up with their support, but they are clueless and referred me here, not such a bad idea I thought!
What is weird:
It was working perfectly fine (lol)
On localhost works
Believing it was one of the recent updates, after a while of debugging I thought, "screw that, just roll back to the last backup" which was working perfectly fine!
However, to my surprise the working backup, isn't working anymore, I'm 100% sure it was working, since I continued working for days, which couldn't have been done without working routes.
The nginx error log is empty, the access log is showing [05/Dec/2021:17:02:30 +0000] "POST /api/search HTTP/1.1" 404 194 "https://domain.io/" - this is the proper route.
I'm already typing more than most of the questions here, please guide me and I will provide anything possible.
Also I heard express is "dead" should I just switch to something else in hopes it will 'solve' this?
THANK YOU!
Updates:
tried sudo apt-get update, removed node_modules, reinstalled all
tried to rename the homepage route from app.get("/" to app.get("/home" and tried to access domain.io/home and it didn't work! On localhost it works fine
reboot server, restart everything, as I said even restored working backup, this issue is beyond me...
Update 2:
I tried a different file, and installed Fastify and tried a default config of
// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })
// Declare a route
fastify.get('/', async (request, reply) => {
return { hello: 'world' }
})// Declare a route
fastify.get('/test', async (request, reply) => {
return { hello: 'test' }
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
The homepage route works, but /test doesn't (even though it works on local) I'm 99.99% sure this is a hosting issue, what do you think?
UPDATE 3:
FINALLY GOT IT, someone please explain how this is possible, inside my nginx config, this line was causing it, after removing everything works.
try_files $uri $uri/ =404;
Can anyone, please explain to me, how my server worked fine for a week with this 'error' inside the config? Keep in mind I restarted nginx dozens of times, this 'line' worked!
Sorry for venting, but how is this possible?

Using path parameter on API of NextJS

I have a simple pages/api/thumbnail.js file with a handler:
export default async function handler(req, res) {
console.dir(req.params)
// ...
}
On a pure express server, it is possible to have a "path parameter", but on NextJS I got a error 404 if a try to pass some path on my API.
Example:
http://localhost:3000/api/thumbnail >> OK, it works
Now with a path:
http://localhost:3000/api/thumbnail/something >> ERROR, 404 not found!
Using NextJS API is it possible to use path parameters or only query string?
PS. I am using Vercel, if using a rewrite is the only solution, its acceptable to me.
You can try adding three dots on the file name like this: pages/api/thumbnail/[[...params]].js Then you can load the params in your handler from req.query
Source: https://nextjs.org/docs/api-routes/dynamic-api-routes

add prefix for all routes in nodejs

I am deploying a nodejs project on a server with apache2 reverse proxy.
When i type this in apache:
http://mywebsite/folder1prefix/myroute
it redirects me to my node application, but apache use folder1prefix because there are other apps.
The problem is my nodejs use routes like this:
app.all('/myroute', function(req,res) { ... });
Is there a way to tell node to ignore folder1prefix prefix. I don t want to do this:
app.all('/folder1prefix/myroute', function(req,res) { ... });
Because the folder1prefix purpose is for testing and it can change.

How to setup gulp browser-sync for a node / react project that uses dynamic url routing

I am trying to add BrowserSync to my react.js node project. My problem is that my project manages the url routing, listening port and mongoose connection through the server.js file so obviously when I run a browser-sync task and check the localhost url http://localhost:3000 I get a Cannot GET /.
Is there a way to force browser-sync to use my server.js file? Should I be using a secondary nodemon server or something (and if i do how can the cross-browser syncing work)? I am really lost and all the examples I have seen add more confusion. Help!!
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./"
},
files: [
'static/**/*.*',
'!static/js/bundle.js'
],
});
});
We had a similar issue that we were able to fix by using proxy-middleware(https://www.npmjs.com/package/proxy-middleware). BrowserSync lets you add middleware so you can process each request. Here is a trimmed down example of what we were doing:
var proxy = require('proxy-middleware');
var url = require('url');
// the base url where to forward the requests
var proxyOptions = url.parse('https://appserver:8080/api');
// Which route browserSync should forward to the gateway
proxyOptions.route = '/api'
// so an ajax request to browserSync http://localhost:3000/api/users would be
// sent via proxy to http://appserver:8080/api/users while letting any requests
// that don't have /api at the beginning of the path fall back to the default behavior.
browserSync({
// other browserSync options
// ....
server: {
middleware: [
// proxy /api requests to api gateway
proxy(proxyOptions)
]
}
});
The cool thing about this is that you can change where the proxy is pointed, so you can test against different environments. One thing to note is that all of our routes start with /api which makes this approach a lot easier. It would be a little more tricky to pick and choose which routes to proxy but hopefully the example above will give you a good starting point.
The other option would be to use CORS, but if you aren't dealing with that in production it may not be worth messing with for your dev environment.

display html page with node.js

This is my first time with node.js. I get it to display the index.html, but it doesn't display the images on the site or anything else, it ONLY shows the basic html stuff. Here's how I set it up.
There's no apache, php or anything else on the server, just ubuntu, proftp and node(and curl and the other dependencies). I made the main directory for the node files /var/nodeFiles and the directory for the html/site files is /var/nodeFiles/www
so for my node server file I did it like this:
var http = require('http'),
fs = require('fs');
fs.readFile('/var/nodeFiles/www/index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(80);
});
this works, but it ONLY shows the index.html file and NOTHING attached to it, so no images, no effects or anything that the html file should display. The files and directories are all correct, I've double checked and the permissions of the folders are correct. So what else do I have to do to get node to display the rest of the site?
I hope I've explained my self correctly, I was told this is the place to ask development questions.
Thank you for taking the time to read this.
but it ONLY shows the index.html file and NOTHING attached to it, so no images,
no effects or anything that the html file should display.
That's because in your program that's the only thing that you return to the browser regardless of what the request looks like.
You can take a look at a more complete example that will return the correct files for the most common web pages (HTML, JPG, CSS, JS) in here https://gist.github.com/hectorcorrea/2573391
Also, take a look at this blog post that I wrote on how to get started with node. I think it might clarify a few things for you: http://hectorcorrea.com/blog/introduction-to-node-js
Check this basic code to setup html server. its work for me.
var http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
This did the trick for me:
var express = require('express'),
app = express();
app.use('/', express.static(__dirname + '/'));
app.listen(8080);
If your goal is to simply display some static files you can use the Connect package. I have had some success (I'm still pretty new to NodeJS myself), using it and the twitter bootstrap API in combination.
at the command line
:\> cd <path you wish your server to reside>
:\> npm install connect
Then in a file (I named) Server.js
var connect = require('connect'),
http = require('http');
connect()
.use(connect.static('<pathyouwishtoserve>'))
.use(connect.directory('<pathyouwishtoserve>'))
.listen(8080);
Finally
:\>node Server.js
Caveats:
If you don't want to display the directory contents, exclude the .use(connect.directory line.
So I created a folder called "server" placed index.html in the folder and the bootstrap API in the same folder. Then when you access the computers IP:8080 it's automagically going to use the index.html file.
If you want to use port 80 (so just going to http://, and you don't have to type in :8080 or some other port). you'll need to start node with sudo, I'm not sure of the security implications but if you're just using it for an internal network, I don't personally think it's a big deal. Exposing to the outside world is another story.
Update 1/28/2014:
I haven't had to do the following on my latest versions of things, so try it out like above first, if it doesn't work (and you read the errors complaining it can't find nodejs), go ahead and possibly try the below.
End Update
Additionally when running in ubuntu I ran into a problem using nodejs as the name (with NPM), if you're having this problem, I recommend using an alias or something to "rename" nodejs to node.
Commands I used (for better or worse):
Create a new file called node
:\>gedit /usr/local/bin/node
#!/bin/bash
exec /nodejs "$#"
sudo chmod -x /usr/local/bin/node
That ought to make
node Server.js
work just fine
You can simply use
res.senFile('PATH_TO_FILE');

Resources