Does code within NextJS API feature exposed to client side - mysql

It is well known that nextjs API routes provide a straightforward solution to build your API with Next.js. and that any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page.
I have just one doubt: is the code within the pages/api exposed to the world? I mean, can I build some logic there that has some key that must be hidden or maybe some MySQL connection?

Whether or not /api is in any way exposed to the world I do not know for sure, but according to Next documentation, "they are server-side only bundles."
In general though, for any key/sql connection that you want to run, I would put that into an .env.local file on your machine, a file that gets git ignored and never uploaded, and if you are hosting on Vercel, then use their environmental variables to store sensitive information.
You'd find environmental variables under:
{Your Account}/{Project}/Settings/Environmental Variables
p.s. Also from Next.js docs, I think you'd find this bit on getStaticProps useful.

Related

How to serve static files of a service using path-based routing?

I've been searching for quite a while on many forums, how to serve static files referenced by HTML pages like href="/", when the frontend service isn't located at the root / of my host, using Nginx Ingress.
This question has been asked many times here, but briefly: I have a example.com/ host, and I need to expose a frontend service in a path like example.com/front1. This simply cannot work. The index.html returns perfectly, but then a href="/styles.css" hits the gateway asking for example.com/styles.css instead of example.com/front1/styles.css. And nginx just returns a 404.
The only "clean" solution I've found is to manage one subdomain for each frontend service, so it would request something like front1.example.com/styles.css. I'm afraid it might not be a great solution.
Some people do hacks like using sub filters annotations to replace href="/ strings with href="/front1/ for each file returned by that ingress and stuff like that, but this also falls short with dynamically javascript-generated pages and so on. And of course I can throw all my stuff in a S3 server, but think about it: If I have a cluster of many microfrontends and other third-party self-hosted frontend services (like signoz, which I have no control on the source code), it would be amazing if I could reference every service by its path like example.com/service-name/ and not worry about anything else.
I've been thinking of a possible solution in a lost comment of using nginx.ingress.kubernetes.io/server-snippet to inject some njs logic to replace the base URL by the Referer header (http://example.com/front1) whenever it references a known service, but I have no idea of how to use njs, if it is even possible, let alone if it is a good solution.
Anyway, my question is, has anyone solved that problem and could point me in the right direction? It is for a friend

How can the default route be removed on cloudfoundry using the manifest.yml

I define a route for my cloudfoundry app in its manifest file:
---
...
routes:
- route: example.com
My route gets successfully created during 'cf push' but unfortunately the default route gets created as well, i.e. I have two routes and I want only the one I defined in my manifest.
Any ideas how to get rid of the default route?
I know it is possible to remove routes, but as 'cf push' is triggered via a jenkins pipeline in my case I'd like to define everything in advance in the configuration. I assume that the manifest file is the best place to do so.
Even better would be if I can move the definition of the route into a manifest-variables file, but I'm trying it in the manifest file directly at the moment to be sure that this is not the source of my issue as I don't have much experience with it yet.
Thanks and beste regards,
Yvonne
This should work, I just tried this against Pivotal Web Services, for which CAPI is always kept pretty up to date (currently API version 2.142.0) and it worked just fine, only creating the route I specified. Which version of Cloud Foundry are you using? Also, I haven't checked if there is such a configuration but maybe your Cloud Foundry instance is configured to always create a route on the default shared domain.
One of the other possibilities is to use the commands unmap-route and map-route to map and unmap the routes of a specific application.

Parsoid - parse wikitext locally

Is that even possible?
I am not sure, if I understand the project properly. I am trying to parse a big amount of wikitext into html using the Parsoid-JSAPI project.
Parsing works fine, but it is still calling the wikimedia API. I have run the server localy, but the library is still using the public internet API instead of my local server.
If i try to specify domain, calling Parsoid.parse("wikitext", {domain: 'localhost'}), it says No API URI available for prefix: null; domain: localhost
My config.yaml:
mwApis:
uri: 'http://localhost/w/api.php'
domain: 'localhost'
Parsing wikitext is possible, sure; that's what Parsoid does. Parsing Wikipedia content is not possible (without API calls) as 1) templates and other transcluded content needs to be resolved and 2) some of the markup is managed by extensions and Parsoid defers to them.
You can set up a local MediaWiki instance, set up all the required extensions, and import all the relevant pages (there is an "include templates" option when exporting content) but it's a lot of effort.

Best practice for handling api key in Play framework

I am trying to build a website with the Play framework that will need to request some directions from the google maps api.
For those requests, I need to use my google api key. Obviously, I don't want to hardcode this value for multiple reasons. Is there a specific file configuration file that is suited for this usage in the Play framework?
It sounds like you don't want to hardcode your key for security reasons - ie. you are checking into a public Github repository, in which case you have a couple of options:
I usually use an environment variable for local development:
maps.api.key=${?MAPS_API_KEY}
ie. where MAPS_API_KEY is an environment variable configured on your machine.
For production deployment I either:-
1.) Set the value dynamically at build time from a private property file. Usually my approach if using a CI tool for the build.
2.) You could also pass it in as a system variable at startup:
/path/to/yourapp/bin/yourapp -Dmaps.api.key="YOURKEY"
You will need to modify your startup script if using activator dist to create your distribution.

How to configure Netbeans code entry point when you use mod-rewriting

I am developing a website in PHP and I am using mod-rewrite rules. I want to use the Netbeans Run Configuration (under project properties) to set code entry points that looks like http://project/news or http://project/user/12
It seems Netbeans have a problem with this and needs an entry point to a physical file like http://project/user.php?id=12
Has anyone found a good way to work around this?
I see your question is a bit old, but since it has no answer, I will give you one.
What I did to solve the problem, was to give netbeans what it wants in terms of a valid physical file, but provide my controller (index.php in this case) with the 'data' to act correctly. I pass this data using a query parameter. Using your example of project being the web site domain and user/12 as the URL, use the following in the NetBeans Run Configuration and arguments boxes. netbeans does not need the ? as it inserts that automatically, see the complete url below the input boxes
Project URL: http://project
Index File: index.php *(put your controller name here)*
Arguments: url=user/12
http://project/index.php?url=user/12
Then in your controller (index.php in this example), test for the url query param and if it exists parse it instead of the actual Server Request, as you would do normally.
I also do not want the above URL to be publically accessible. So, by using an IS_DEVELOPER define, which is true only for configured developer IP addresses, I can control who has access that special url.
If you are trying to debug specific pages, alternatively, you can set the NetBeans run configuration to:
http://project/
and debug your project, but you must run through your home page once and since the debugger is now active, just navigate to http://project/user/12 in your browser and NetBeans will debug at that entry point. I found passing through my home page every time a pain, so I use the technique above.
Hopefully that provides enough insight to work with your project. It has worked good for me and if you need more detail, just ask.
EDIT: Also, one can make the Run Configuration Project URL the complete url http://project/user/12 and leave the Index File and Arguments blank and that works too without any special code in controller. (tested in NetBeans 7.1). I think I will start using this method.