How to perform CRUD operations using angularjs, nodejs and mysql? - javascript

I am trying to understand how to perform crud operations using AngularJs, NodeJs
add Mysql. I need to understand the approach for this. Please explain with some
basic example.

CRUD is the acronym for the set of operations Create, Read, Update and Delete.
CRUD is the basic set of operations required to use and manipulate a collection, or resource if you will.
In this setting, "performing" CRUD operations would relate to your application in the following way:
Angularjs uses $http to run http requests asynchronously against your RESTful API
Nodejs implemest the routes that makes up the API, hopefully in accordance to the best practices widely used in existing APIs (An in-depth blog can be found here. Based on the route/api-method triggered, the nodejs will also send queries to MySQL to persist the changes nessesary.
MySQL's job is to persist the data you are working with. Using SQL queries to (C)reate, (R)ead, (U)pdate and (D)elete data (CRUD operations). MySQL ofcourse has a lot of other tools as well, but in reference to the CRUD operations, these are what you need.
Now this is just the practical idea of how to implement a RESTful API with CRUD operations, persisting it with MySQL and consuming it with Angular. If you really are serious about learning more about this, you should:
Google better (Really, it's too easy to find info about the subject as people have mentioned). The terms REST, RESTful, API, CRUD are all good keywords to google, then append whatever keywords you are more interested in "Best practice REST API" for example.
Read blogposts, follow engineers on social media/blogs, read books, watch youtube etc. This information is everywhere.
One important thing that might not be obvious when starting out: Always try to find and standards or best practices for what you want to do. That means someone already has done the hard work of trying and failing, and basically serves you the best solution on a silver platter.

Related

Where can I start to create a REST API's that will manage job listings?

I have an assignment to create a REST api's that will manage job listings and possible applicants, the program must be backed by an SQL database and I am a bit lost on where to start could someone please help me?
you can approach this in a lot of ways,
first of all you need to choose a backend language to use, this choice can either be driven by what languages you can actually use or some project requirements
here are some frameworks by popular languages:
Java: Spring Boot
Python: Django, Flask
C#: .NET Core REST Api
Javascript: Node.js -> Express.js
then you need to decide if you want to use some tools like SwaggerUi to show the endpoints
also depending on the level of complexity you want to achieve you may need to consider using an open relational mapping library (ORM) to handle the queries for you, an example for C# is EntityFramework, for other languages you can easily find equivalents by searching ORM
you are gonna encounter some CORS problems probably so do some research on that topic too, each framework has his own ways to handle CORS
I'm not gonna cover the security part on this answer but if you need to provide conditional access to the APIs you need to consider that too as you will need some way to check if the request is authorized, as this is usually done using Bearer tokens a quick research on that won't hurt!
A very popular framework is Spring Boot: https://spring.io/guides/gs/spring-boot/
You can build REST-Endpoints and also connect to a MySQL Database using JPA.
Many different programming have the capabilities to create RESTful APIs like:
NodeJS
Python
Java
.NET
They all have different frameworks to create APIs, with different structures.
My recommendation would be to use a programming language you are familiar with, if none, it would be best to use Python, it is easy to learn with English-like-syntax.
Secondly, depending on how you want to structure your project, it would influence the language to use. Some factors like speed of the project or functionalities on the backend might influence the programming language you want to use. As well as integration with other languages.

is it possible to built REST API without knowing about frontend for mobiles apps

This could be a silly question but I want to confirm about it. I am a backend Laravel developer and have created my website. Now I want to create mobile mobile apps but have no knowledge of front-end.
Due to lockdown situations I want to use this time to create the backend API for my apps. Now I want to know is it ok or a normal practice to create APIs without knowing anything about front end. (Like is it possible that I just create all the endpoints now that are throwing data in JSON and then the front-end developer will use them to integrate in the front-end? Thanks
P.S: I dont know how the app front-end will look like but I know what data will be needed in each page/view.
I think you can do this easily with graphql.
A Frontend dev and also beginners with Apollo in the frontend can easily introspect and build the stuff.
If you can choose your Frontend stack there is no problem with rest either.
I always do the Frontend first approach.
Mocking the data for graphql types and building the api later seems to me the best workflow ever.
See ya. Much fun with your project
Yes it is possible very possible to do that it would be nice is the API is well documented using postman or swagger for a better experience when frontend engineers want to integrate
Many API's in the world today are never consumed by a browser or app on the frontend, sure it can make life easier knowing why you are building it.
But at the end of the day, a good API is simple and easy to understand. It just allows a developer to fetch data from a system but normally with some idea about each type of request.
For example, if you had an API for a university the endpoints could be as follows:
/pupils => Get/Sets pupil data.
/teachers => Get/Sets teacher data.
/classes => Get/Sets class subjects and references ID's of both Pupils and Teachers.
So if you wanted all the Pupils in a given class you would query the /classes endpoint and then for each Pupil ID you would query the /pupil endpoint.
Sometimes not having a frontend makes the API even more generic because you might think differently when you do not have a frontend.

JavaScript: Learning how to "consume RESTful APIs"

I've been coding JS for a few years, and am now learning more. Seeing a lot of employers asking for "knowledge of REST APIs" or "experience consuming RESTful services."
I know the basics of AJAX, both in native JS and jQuery. I've done a fair bit 'o goggling on REST, both on SO and the web. There seems to be a ton of info on how to build RESTful APIs server-side with JAVA, C#, etc, but little on how to access those services with JavaScript.
Here are questions:
Is "consuming RESTful services" another way of saying "getting data from a server with AJAX", or something else altogether?
If it is something else, where are some good tutorials on the subject?
Once I get the basics down, where can I find RESTful APIs on the web to consume?
Is "consuming RESTful services" another way of saying "getting data from a server with AJAX", or something else altogether?
No, not at all, but you certainly could consume your own REST API using AJAX. Ajax typically just means xmlHttpRequest, which is inherently a web browser concept. A RESTful API is more like a service interface. In many ways, it's a web service, but without the complexity of SOAP.
Essentially, REST is a way of expressing a transaction or query over HTTP by using verbs and nouns. For example, to get information, you use... wait for it... GET! To put information, you use PUT (or POST, the tutorials below will explain the difference). And to delete something, you use DELETE. It's rather neat. It almost reads like a sentence over HTTP:
GET /users/123
DELETE /users/123
You can guess what those would do.
Because these are just HTTP requests, they can often be consumed using AJAX, however, that is not necessary. In fact, I find REST API's more useful for exposing services or data to other applications, again, just like a web service.
If it is something else, where are some good tutorials on the subject?
It's hard to give a good tutorial on consuming RESTful services because of two reasons:
Each service is different.
It depends on what language / platform you are working in.
Bret's answer mentioned a few good tutorials.
Here is a SO answer which shows a way to consume REST directly in Java: How to consume REST in Java
Here is a reasonable tutorial on how to create a RESTful API using the JAX-RS API in Java and then shows how to consume it: http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
I've also had good success with Jersey's REST client. Their REST (JAX-RS) server software is a royal pain (in my opinion), but their client is pretty slick.
I wrote a simple proof-of-concept todo list REST API last spring in Java and the integration tests show good use of the Jersey client to consume the API for testing purposes.
https://github.com/brandonramirez/todo-api/blob/master/src/test/java/com/brandonsramirez/todoApi/TaskResourceTest.java
I'm also a fan of the Unirest client, which is a REST client translated into several languages with a similar interface. They have an Objective-C version, a Java version, a JavaScript / node.js version, a .NET version, etc.
If an employer is looking for experience with consuming REST API's, they probably want five things:
Do you understand the basic semantics of exchanging data over HTTP using the various methods and the design patterns, naming conventions, etc. that go with it along with it?
Do you recognize the de facto standards for mapping CRUD operations onto HTTP requests (like using GET /resources to list all resources, POST /resources to create a new resource, PUT /resources/x to update resource X, etc.)?
Are you familiar with the various HTTP response codes and when/how they would be used? Like can you always assume that a successful response yields a 200 status code? If you create a new resource, is there another commonly used status code (yes, there is)?
Have you actually written any code to consume an existing service?
Can you test a service easily? For example, using curl to create issue an HTTP request very precisely.
And a bonus, but most people don't have a good grasp of this (I struggle to understand its usefulness personally), is hyper-media for versioning by twiddling with the Accepts header.
Once I get the basics down, where can I find RESTful APIs on the web to consume?
All over! Everybody has one! Twitter, Facebook, Twilio, Stripe, Google, Edmodo, Amazon. I could go on forever. In the IaaS world, Amazon Web Services and Rackspace Cloud both offer RESTful API's for managing your cloud infrastructure. For example, I can make an HTTP POST request to a URL that represents my set of servers, and boom, a new server (virtual machine) is provisioned to me.
https://stripe.com/docs/api
https://api.imgur.com/
There are so many that there are even hub services to help consumers find API's and providers to publish them, such as Mashape and Mashery, though the latter appears to be more focused on providers than consumers, judging from their web site.
The github project I posted earlier, with my proof-of-concept, accesses a REST API from Searchly and Twilio so that I can search for tasks, and when one is marked complete, send me a text message, again, all from HTTP requests. However, I used the client libraries from those providers rather than using direct HTTP libraries just to make the code leaner and more focused, without worrying about all of the HTTP plumbing.
For your purposes, hitting a restful API will be very similar to interacting with a server via regular AJAX. The difference is that this API will conform to the restful model, where various CRUD-like operations will conform to REST's particular pattern of using specific HTTP verbs [GET, PUT, POST, DELETE] and also its pathing model of referring to an entity.
Have a look:
http://www.restapitutorial.com/lessons/httpmethods.html
If you want to have a play with a restful API, there's a few frameworks out there where you can spin up your in a few minutes.
I'd probably use Sinatra for this; although there many options:
http://www.andrewhavens.com/posts/20/beginners-guide-to-creating-a-rest-api/
Just google "restful api example [your server-side language of choice]; there's a lot of material out there.
In short order you'll want to explore how cross server side scripting is done with a restful API, and how to use JSONP:
http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

BreezeJS REST saveChanges

I've been pouring over BreezeJS documentation and stackoverflow posts looking for definitive information on using BreezeJS to interact with a REST tier (not .NET) that supports CRUD operations. I've read a number of posts that clearly communicates the philosophy behind the default behavior of saveChanges() which sends a collection of updated entities to the server. While creating server-side code to handle this may be possible it is likely that won't be our preferred path and yes, I do understand that there are transactional and state issues that decision would inflict on the client code.
In my searches on stackoverflow I've found a number of questions that are similar to mine, but those questions are all relatively old (at least for JavaScript libraries) - (ex. Save changes to RESTful URLS with Breeze JS).
Some of those posts, including the one above, seem to indicate that work is/has been done to address the desire to do CRUD operations via normal REST operations.
Finally my questions: What is the current state of BreezeJS with regard to supporting RESTful CRUD operations? If I choose to manage the entity updates via a named save operation what kinds of hoops will I need to jump through to get Breeze to send those CRUD operations to my REST tier? What else am I missing in my summary above that will make CRUD operations from BreezeJS to a REST tier challenging?
Note: our JavaScript framework is Angular.
I do understand your question and your perspective. I haven't had time to document how to do this yet.
You'll find clues in the "breeze.ccjsActiveRecordDataServiceAdapter.js" in the CC-JS Ruby sample. That adapter is easy to examine in github.And also in the "breeze.labs.dataservice.sharepoint" adapter which you can examine in github.
Both adapters target servers that want PUT/POST/DELETE to specific per-type endpoints (and do not understand "batch saves").
Intend to do a thorough presentation and sample for these "REST CRUD" scenarios "soon" ... but probably not before May.

AJAX Web Application Design

I'm working on a personal project to build a small web app that is built using AJAX requests and talks to a RESTful API rather than traditional HTML pages and form submissions.
Are there any online articles or tutorials or any books that people could recommend that cover design patterns for this kind of thing?
Finished my part of a similar project on Friday which included using WCF endpoints to build parts of pages (we were calling them pods). So the pages had boilerplate and then ajaxed in the pods. From your description I think you will need a 'behavioural' pattern as they deal with the patterns of communication between objects.
The design pattern we started with was 'Chain of responsibility' (from GOF book):
"Avoid coupling the sender of a
request to its receiver by giving more
than one object a chance to handle the
request. Chain the receiving objects
and pass the request along the chain
until an object handles it"
Although I don't know if this covers your precise requirements.
You are better off googling than asking here. There's a ton of stuff out there.
I recommend using jQuery to help with the client side aspect of AJAX. It does a lot of heavy lifting for you and integrates well with forms, the DOM, etc.

Categories