How to make JSON response from server side - json

i would like to ask this for my second project of web apps, we were told to design and create javascript app that gets the information from a pre-defined database, and allows to add more data to it, modify it, erase any register, and search. That's pretty easy, but the thing is that our teacher asked that in the moment the browser makes the query to the server and this access the database, the server should return the response in form of a JSON.
I do not know how this is done, thats why i'm asking for a bit of assistance, since i always saw JSONs like libraries that can be add to a Javascript app.

You should pick a technology for creating a web accessible server api.
One common one in the .Net world is WEB API. You can also look into NodeJs (maybe using Express).
Json is really just a data format. It serves the same role as xml, but using a different notation.
Some frameworks like Asp.net Web API do content negotiation and returns data in the format requested in the http header (content type). Other solutions might require you to manually serialize the return data into JSON.
Link http://www.asp.net/web-api

Related

how do i store and retrieve form data from a wesbite

this is probably something i could have googled; however i thought id start getting more active on chat forums so basically...
I'm almost done with making a family members business website, on the website is an enquiry form; i want to know how i go making that form store the data its given and how i would then retrieve that data?
still doing final touches to the design then im going to start learning the process of deploying it live which is why im posting here to see how to go about it.
It is very vast question, You will need an API to pass the data from Forms to API Server and a Database, that will store the data. It is also called client-server architecture.
Web Forms -> API Server -> Database
You can use either node express, java, python or any language to create API.
You can install the database on the server where you will run the API or you can use any from the cloud like AWS or Heroku, that way you will skip some setups and it will be ready to be used directly in the API.
To store data from an HTML form, you will need a server-side language and a database to save the data to. Some popular options for the server-side language include PHP, Java, and Python.
Once you have chosen a server-side language, you can use it to write code that will process the form data when the form is submitted. This typically involves connecting to a database, creating a table to store the data in, and then inserting the form data into the table.
To retrieve the data from the database, you will need to use a SQL query to fetch the data from the table. You can then use the server-side language to display the data on the website as needed.
It's a good idea to also consider security when handling form data. Make sure to validate and sanitize the form data to prevent any potential security vulnerabilities.

Is accepting a json directly from the client insecure/bad practice in a Golang app with a json file datastore?

I have a simple Golang application with a HTTP API that controls the app, the api has a frontend over it powered by a React.js app. The frontend allows users to change settings and POSTS a json object called settings directly to the API /api/settings endpoint (which has simple JWT authentication builtin). I take that object unmarshal it into a struct for the application to use, in case of an error the application uses the default struct and sends an error to the client. My question is; am I doing this correctly or is this insecure; sending the json directly to the app, without any server side validation, but a simple json.unmarshal?
PS:I come from a PHP+MySQL web apps background, where accepting client input for the DB without escaping it was a very dangerous thing to do.
In principle, yes it is bad, however you would need to decide whether the risks outweigh the cost in your specific case as the work required can be quite extensive.
No matter the language used employing the model 'Never trust the client' tends to provide the best security. In this model you assume all requests are malicious therefore should be validated and sanitized. This allows you to gain confidence in the data you are storing, and in principle protects against changing requirements. For example; if you are presenting this stored data back to the users then you can have some trust that nothing malicious is stored, however this should not mean output encoding is not also used.
The final point to consider is how you are accessing the DB and creating the inserts. Assuming this is a SQL DB then ensure your query uses named parameters rather than string concatenation.

Creating a REST API for static hosting

I know this sounds crazy, but I had a thought and I was willing to try it out. I use GitLab pages for all my online projects, but a lot of them are ASP.NET MVC, which is an issue as I don't think you can run ASP.NET MVC sites on GitLab pages. I then thought, what if I make a site using something like angular or node.js, and have a central API for all my web projects? I thought that was a great idea, until I realized I couldn't use a database either. I guess what I'm asking is, would it be possible to create a REST API that uses JSON files for storage and node.js as the request pages, to create an API without a database?
Of course.
If you think about a database from the perspective of your application code, it is basically just a place to store and retrieve data.
Imagine the database library you are using has two simple methods, store and retrieve. In your application code, you could write db.store('here is the item') and the later on, db.retrieve().
However, those store and retrieve methods could be implemented in many different ways to provide the same effective behavior from the perspective of your application. Some examples:
Send/query the data to/from an external data store, such as PostgreSQL
Write it to a file on disk and read it back later
Store the data in memory
Make HTTP requests to an external system to store the data
Some of these options will be more or less appropriate depending on your exact requirements, however, the general idea is that given a database API, you could implement the exact same method signatures with a completely different approach.

is angularjs working with database or json?

I am developing web application. I am getting data in json or database. I can use this data in angularjs at one time (i think so). So if any data changed in json or database then angularjs should work .Is this how it normally works? Is it possible?
thanks in advance.
Yes and no. When we say data binding in Angular JS, we are referring the data in memory and manipulations we do to the data. For instance when we type in text field, we update a javascript object, and then display it in another form on the browser.
When dealing with external data, e.g. json or database, we will need to fetch that data from the server. Browser on the client side won't know that json has been changed in the server, it needs to send a request to the server to fetch new data. After the data is loaded into memory, then we can do the same manipulation and display it.
The remaining question is when to trigger the data refresh. Well this is not an easy question for web application if you are using restful API. It can be reactive like when user do specific action, or refresh at fixed interval, depends on your requirement. I heard that socket programming is good for this kind of thing but I'm not expert in it so I'll leave it to others.
Angular apps usually manage their data using RESTful API endpoints. This means, that your Angular app communicates (usually via JSON) with a backend application running on a server, which handles all database interaction.
In practice this means that to get for example all blog articles, in your Angular app you would do a $http GET request to api.yoursite.com/articles. Then your backend application does database query and returns a JSON with all the articles.
Does this answer your question? Because it wasn't clear what exactly you were asking.

Ruby on Rails Pulling Information From Another Site

I have a ruby on rails project where I have to make a press page but I don't just want to put the articles in the database (that will take too long) is there anyway that you can pull information from another site and display it on your own site.
This is quite a broad question.
Depending on whether the web applications that you're getting your data from have APIs or not, you would either use those to retrieve the data or you'd have to web scrape it.
For accessing an API you could use Net:HTTP or HTTParty or similar. For web scraping you'd want to use similar libraries for retrieving the page and some parser like Nokogiri to parse the HTML. In both cases you'd want to store the results in your own database to avoid having to scrape/query API upon every request.