Accessing JSON through Javascript- Different options - javascript

Still a newbie. I am building a simple quiz app which I plan to take make public. The questions will be stored in a JSON. Trying to understand the right approach to build this:
The javascript file cannot read the local json file through fetch (URL scheme file not supported) or using jquery (I get CORS error)
I want to expose only part of the json file and different question will be shared everyday.
Do I need a nodejs server to address these requests? What is the right way to host this on a server? When I buy a domain, do I need to keep a server running there to service these requests?
Would appreciate a response on the approach.
Thanks,

There are different "Components" in building and deploying an application on the web.
To handle the User Interface (Website as we know it), we would require HTML/CSS/Js or a frmaework like React, Vue, Angular etc.
This frontend will be connected to a server (backend) that can be developed by using Nodejs(Express), Python(Flask), Java(Spring), Go etc...
Then there is a database, you can use SQL or noSQL databases. (As per your requirements, I would recommend using MongoDb Atlas, a hosted cloud database)
Initially, you are going to develop Front-end and backend seperately as deifferent projects. Then Integrate the APIs.
Now, you can either host these projects seperately, or together.
Now, for testing servers, you can use Heroku or Netlify, these services host your projects for free (upto a certain limit), once you are done with development and testing. You can pay these services or choose other services to host your project and do other stuff like a good domain-name, a SSL certificate etc.

Related

Using Active Directory (with LDAP) to authenticate on an angularjs/javascript fronted - what should the flow of the process be?

I'm working on a project in which we need to authenticate the user in an application by using his/hers windows credentials. Frontend is using Angularjs and backend java.
After doing a sensible amount of research, I discovered that there is no way on the the frontend to obtain directly the Windows user & pass due to security concerns.
But I'm thinking that the whole process should start here, from the frontend, by obtaining these two encrypted credentials or at least a token and sending them to the backend in order to kickstart the ntlm authentication process.
And also, not sure if the user should have to log in the app by typing his windows credentials or if it should automatically be done with ntlm passthrough.
I do not have a good grip on the concept, and that is because most of the related sources that I found are referring to backend solutions (C# 80% of them), but almost nothing for fronted. So, I humbly require some clarifications on this topic. Is there some sort of middleware or npm package that I should use in order to obtain the user & pass, or what would you advise?
Web servers expose certain server variables to code handling requests they serve.
For me, using IIS, these are described here: https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524602%28v%3dvs.90%29
Since I am using IISNode; my node.js environment is completely embedded into IIS; I have access to these server variables. As the link described, each programming language seems to have their own way to access these variables.
So I would doubt it if Java does not have those as well. The exact code to use will depend on you back end.
But a quick search for "java server variables" already yields me the following:
https://docs.oracle.com/cd/E19534-01/820-3288/gawim/index.html for the java access manager.
http://users.polytech.unice.fr/~buffa/cours/internet/POLYS/servlets/Servlet-Tutorial-CGI-Variables.html for old school JSP.
How can I obtain server variables using apache wicket 1.54? for java wicket server.
So have a look at the documentation of your specific web server software or Java API.
There should be a list and example code of how to access these.
Once you obtain this data server side, you can do the LDAP query and then return the results client side.

Is a Spring Boot microservice with a non-Java front-end client possible?

I've implemented the shell of a microservices-based REST API application. I have simply followed the guides on Pivotal Springs' own documentation using Eureka and Ribbon for load balancing. Everything works. I have a discovery server with a handful of independent services which can register with the discovery server.
Now, my problem is that I might prefer not to write my client-side app in Java - maybe Angular or node.js, etc. However, the load balancing and connecting to the discovery server is all done in Java in the examples I've followed.
Is it possible to use JavaScript to do the same things that the Eureka client does with the Spring Boot microservices so that I don't need to be constrained in my choices of browser client technology? Does anybody have any advice for how this should be approached? I had difficulty finding any articles that cover this, to be honest.
Yes. Definitely you can choose technology of your choice for developing front end application. From your front end application, you make calls to API endpoint that you expose via your spring boot application.
You might want to expose your services via single API gateway that will help you route requests to designated micro services using your discovery server.
Actually you should not be doing load balancing/service discover etc. in the front-end. So the question about whether it is possible in JavaScript or with which libraries is irrelevant.
Typically you'll have an API gateway or a (load balancing) proxy which works with your service registry and routes requests accordingly. In the current project we use Consul for service registry and Nginx + consul-template as proxy. We plan to migrate to some API gateway.
With this setup your front-end will connect to just one central endpoint which would do load balancing/routing to individual service instances behind the scenes. Thus your front-end will not need to implement anything like Eureka/Ribbon etc.

Single Page Application (SPA) vs Full Stack Applications. Constraints and Advantages.

Currently I am in the process of upgrading our web application from a traditional Spring MVC web application to a single page app with REST endpoints. Our current front-end MVC application does not use REST calls to communicate with the backend, but rather communicates with the backend (written in JAVA) by calling the necessary facades directly. Both JAR files and WAR files are being packaged in a single ear file, and deployed on our production server (currently using JBoss EAP 6).
Since we are now shifting to a single page application, and upgrading our system with a new set of APIs, I am questioning, should the Single Page Application, and the rest of the backend written in JAVA, be hosted on the same server (JBoss EAP 6)? Or should they be split onto separate servers, one to serve the SPA content and one to run the backend? In the latter case, which production server is most appropriate to host the content of the Single Page Application (JS,HTML,and CSS)? ( our backend will still be hosted on JBoss EAP 6 )
Also what are the advantages of separating the front end and backend on different servers?
I tried to search for best practices for deploying Single Page Applications with a JAVA REST endpoint but I haven't found any helpful articles applicable for our needs.
Thanks a lot in advance! :)
To answer your first query:
Yes you can absolutely separate them, and ideally you should so you can deploy the front end without dependency on web-service back-end.
You can deploy your SPA static files with any of the popular web servers like Apache, Nginx, or even on cloud hosting like S3 (behind cloud front CDN).
Assuming your REST endpoints are still going to be in Java, those would need to reside in Java application server like jBOSS, tomcat or glass fish.
Constraints/Gotchas :
Cross domain:
You can either put your JBOSS behind same Apache/Nginx reverse proxy that runs your static files.
Or you can enable CORS on web services if your domains are separate.
Lastly, jsonp is always an option if your web-services are JSON.
Authentication and Security:
Typically when you go with a full stack web framework like Spring you get a lot of security and authentication out of box. You can protect your site using sessions and CSRF etc. However with REST you typically have to use Token based authentication for your front end to speak with REST services. This is not necessarily difficult, but a different approach, and is hence listed under constraints.
Advantages:
Easier to scale the back end and front-end separately, with static SPA on a service like amazon S3 and CloudFront CDN you can infinite scaling on that part.
The back-end web services can now be easily put behind a load balancer-cluster model because your services are REST.
Its way easier to take care of deployments now because of separation of concerns.
Lesser regression issues when pushing just front-end changes. You don't have to replace entire WAR file any more.
Separate Servers or not
Depends on what kind of traffic your application is expected to handle. Let me lay down three scenarios.
Low traffic: You can put it one server with Java App server behind that Web server's reverse proxy. The web server will also serve the SPA some directory.
Moderate traffic: You should separate the fronted and Web Server on one web server and have REST services hosted on Separate machine. Technically this setup won't be much different from option 1, but your App Server will not compete with Web server for CPU cycles to respond to requests.
High traffic: Same as option 2, but now you can have multiple App servers and SPA Web servers and have one Apache/Nginx to load balance on the top.
Insane traffic over wide geographic region: You don't want to host those SAP your self in this case. It would be better to go with a a service like Amazon S3 behind CloudFront CDN, so your static content is replicated across the world for optimal response times. This would also reduce that load on your servers. Now about the App server for hosting the REST endpoints. You can either go with your own servers in cloud that host your cluster or go with PAAS like Heroku or amazon to host your WAR files and scale on demand.
Note: these scaling scenarios don't account for database, as you would need more information about your database to figure how to scale it in the above scenarios.
Hope that helps, please let me know if you need more specifics on any of the things mentioned.
This is a matter of taste.
My self prefer creating a SPA using a generator like Yeoman and then create a service with either Spring IO own setup for webservices (example here: spring webservice example) or Jersey here.
When it comes to deployment are there multiple setups.
Apache | Nginx | S3. Here is a great article for deploying an AngularJS application: deploying angular app
You will probably still need to use Tomcat, Glashfish or JBOSS ect when it comes to the Java part.
This is not an answer only my taste of choice
Now a days, front-end is hosted mostly on separate servers and back-end services on cloud like AWS, In your scenario you can use FireBase for front-end (Html, CSS, JS, Angular) and sure you can use separate server for back-end services, it is also feasible to use Spring MVC as your current framework with it as a restful services.
Take a look at this demo

How to actually deploy a node.js (or django) project on a hosted web server?

I know this question may seem stupid but I've been googling and asking this question for a while since I decided to start learning node.js. Ok so I have a hosted web server over at X10Hosting (https://www.x10hosting.com/) and I'm wondering how I could host a simple node.js web app on it. Thanks, any help is GLADLY appreciated (an actual tutorial or documentation would be really help).
You can't deploy a NodeJS/Django in this type of server.
When you sign up for a free hosting plan in x10hosting, they give you a folder in their server.
When a request come for you, Apache/nginx (which their are webserver) will execute the corresponding PHP file in your folder and will give to the client the output.
NodeJS is a very different type of installation.
It have is own process which it will bind a port, handle and process the request, and give the client the output. So, you will need to install it on their server, but i don't think x10hosting will do that just for you. And Django work the same way.
Nodejitsu ( https://www.nodejitsu.com/pricing/ ), the "owner" of NodeJS can host your app for free if the application is open-source.
I hope this will help you ;)
x10hosting's free hosting service is a shared hosting service, that means that you're allowed a folder inside a system shared by many users. That system has PHP5 and MySQL (as I read in their website), but Django and Node.js are a very different software stack. See, software stacks like Django or Node must be installed by the system administrator in order to serve content like a website. The administrator provided PHP5 and MySQL, but no Node or Python support. With a VPS, or Virtual Private Server, you are your own sysadmin, and free to install and configure whatever you want in your system, but it's not the case for shared hosting.
The Django Project website has a list, with some shared hosting or private hosting providers that support Django and some of them are free. For NodeJS you should checkout the Node Wiki's hosting list.
If you want to deploy easily you can use Heroku, it's free if you use only one Dyno (You don't need more if you create a website with low traffic)
Heroku and the really good documentation (with article for django and nodejs) : Documentation

How to efficiently develop client-side code for a web app without installing a local backend?

One of my team members is working only on client-side (Javascript) development for a web app with a large and complex backend.
I would like to avoid the need for him install and configure a local copy of the backend.
However, I wouldn't want him to need to push every small change to the dev server just so that he can test it.
We thought about getting the client to make the requests directly to the dev server, instead of to the same domain (the localhost) but this doesn't seem practical due to cross-domain request policies and authentication problems (cookies aren't getting sent).
What are some elegant solutions for developing clients without having a local backend?
Depending on how complicated your backend is, you might be able to create a mock backend using a lightweight web framework like Sinatra. I've had some success with this technique, but the services I've been mocking have been fairly simple. In some cases the mock backend mostly serves static JSON files.
I use Charles Proxy to map the URIs of the dev server's web services to localhost (where I run a light weight web server that serves up my static development code).

Categories

Resources