Use VueJS with sailsjs - node.js

How do i use vuejs with sails js. I want to list and create dynamic content using Vue. How do I create route for CRUD operations for objects like posts and comments ? How do i check authorization on every route that the user must be logged in for this route to work. One more thing , how can I test the crud operations using Postman
Thanks

You have two ways of using VueJs in Sails.
The view layer of sails js is built on Vue.js, so you can use the native implementation of Vue.js as described in sails js doc.
Or, you can setup a Vue.js standalone app, and make it dial with you sails.js backend by the way of http requests that can you send with http, axios, ... packages .
In both cases, you're gonna use the entry points that you will define in your routes.js file of sails > config folder which would corresponds to controllers that contains your CRUD requests. That is also the way you have to test your "CRUD" operations with postman, by requesting your entry points.
For authorization, a good practice would be to use the policies of Sails.js :)

Related

Angular 2 + Node JS for backend and frontend

I am little confuse about how to implement this especially for routing, how i handle routing for backend and front end and how about i using source (css, html , js ) which it will be different between front and back. thank you.
Yes, that can be achieved using MEAN stack application.
Mean stack tutorial guide Click here !!
You Can Choose Angular As your front End Framework to work with user-Interaction that will be user seeing in his/her browser and Node.Js for Backend Interaction to your Angular Application for making API calls and fetching Data that you want not to be shared with users.
In regarding to routing question use Angular Router module that will handle how user move from one component to anthers on your Front-end.
In Back-end you can use Express router module to Handle your routing requests to specific urls.
You can use Angular 2 as Frontend and Node js as your backend.
Furthermore, the routing will be set on frontend to show html pages and to get the data or perform any sql operation or to put your logic you can use node js by creating methods and then calling these methods from angular 2.

How to integrate a Nodejs API with ReactJs app under the same domain

I'm trying to understand how a MERN app fully works, I've been reading about MongoDB, ExpressJs, ReactJs and NodeJs, I also understand how MongoDB, ExpressJs and NodeJs interact and how ReactJs works on its own, my question is simple (I think).
The question:
If I create an API, using Node,Express and Mongo, and I have an APP managed by React, both need a server (via express, I understand), then, how should I run the API and the React app at the same time. Do I need different URLs? should I configure different ports? how should I integrate them?
I really been reading a lot, but almost every tutorial is made locally (and I'm working in a server with Passenger and I can't change the way it starts), just for Node/Express(with pug or else)/Mongo or just React, and I don't understand how to connect the API and React.
Thanks
It depends on several factors: environment (e.g. development, production), and your control over the server. For development, you can have two different URLs and use something like Webpack Dev Server. Normally you would have the module bundler, e.g. Webpack, watching for changes in your React code. However, this can get more complex if you have Server Side Rendering.
For production, normally you would have the bundled file for your client side application already optimized and minified. If you can change your API, you could serve it statically in a new endpoint, for example: /static/bundle.js and request this endpoint from your index.html file, which will be sent by Express.js server when accessing /.
However, because you will probably want to have routes in your React app, your server will need to know how to handle the client app routes (for example app.get('/*', () => ...), and they could collide with your API endpoints. To solve this, you could:
Prefix your API endpoints with a namespace, e.g. /api/v1/...
Place the API in a different URL, port or subdomain. In this case you would indeed need to run these two servers in parallel. With Node.js, there are helpers to make this more convenient, e.g. concurrently.
Pulling out your concerns: API, React, and Integration for MERN app.
I use three approaches
1) Use foreman. With this, you can specify your API and Web Client in the Procfile. I used it here
2) Use a proxy to handle requests that require your API. So in package.json, you specify your API URL(your API must be running)
// package.json
.......
.......
"proxy": "<path to url:[port no if you're developing locally]>"
Check here.
And you can simply add a script to run your API and React concurrently.
3) Set your API and React app in a Docker container. mern-starter is a perfect place to check for this.
Hope this helps!

How to correctly use Nodejs and backbonejs together?

I have an application which uses nodejs as server and backbonejs as the frontend framework. As I know, both backbonejs and nodejs handle the url.
I have a question: for example, let say I have page called localhost/project and a page called localhost/details. When I load either of these pages, nodejs server should be called first, and then in the view.jade I have:
script(type="text/javascript", data-main="/js/bootstrap", src="/js/lib/requirejs/require-min.js")
which initiate the control of backbonejs. So, does that mean everytime that I load a NEW page (localhost/details --> localhost/project), I have to reload all the steps for initializing the backbonejs (requireJS work)?
Unless you have to do it differently, perhaps a better approach would be to use node.js as a REST API. I would use a framework like express. Then, in your Backbone code, hook your model and routes up to call your node API and update the views with the data it receives.
Unless you are doing any kind of processing on the server side, you can skip the node step and run your SPA's right off of Firebase or Parse.

serving an angular directory and subsequently using restful API's to load data when needed with nodesj

I am writing my app and I want to do the following:
On the first request, I serve all the client side code (views, models, controllers, css ...) and subsequently I want to do RESTful api calls to the server to populate my app with data.
I've been looking everywhere but can't find a complete example. Connect serves a static directory but after that I don't know how to route RESTful api requests.
I'd recommend that you use Expressjs on top of Connect for request routing:
http://expressjs.com/api.html
Its a very straight forward framework, with tons of tutorials online. Here is a good one for getting started:
http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

Using server-side php to authenticate javascript calls

I'm using a Symfony2 backend and AngularJS frontend in one project. Symfony is used to show the initial view and provide a RESTful API with AngularJS embedded into the view.
Basically AngularJS will be used for the frontend (view) and calling Symfony API to interface with the database (model).
I have a bunch of Angular http gets and posts which exposes the URL to my API . What would be the best way to secure the API in my scenerio?
I do have access to server-side with Angular but how do I pass that authentication from Symfony to Angular?
Thanks for the help.
This is a common need, and there are several approaches - but they usually settle on using sessions.
You might find this article to be a good overview of an AngularJS-specific implementation...
http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application

Resources