Can Express routes be used as MVC Controllers in Node.JS Apps? - node.js

I have been playing around for a bit on Nodejs, so forgive me if my question might look stupid.
When I setup a new nodejs app with express, I noted that it created a folder /routes that basically does what it suggests.
Now, is it ok to use said file(s) as the Controllers for my app?

I think so. Because role of a route in express is to decide what to do when a certain request comes, such as getting data from a model or rendering a view with some data and this is what controllers do, right?
You can see in official examples that routes are actually placed in a folder named conrollers.
https://github.com/visionmedia/express/blob/master/examples/mvc/controllers/pet/index.js

Related

React and Express Routing and Views

I'm a newbie in web development. I built a project before using Node, Express, and EJS (as my frontend) and now, I am trying to make another one using React as my frontend.
I watched and studied a lot of React + Express tutorial videos in Youtube but I think every video only shows a one-page site.
My question is how do I handle the routing of Express+React? Where should I put the routes and render multiple pages or views? Should I put it in the app.js under the Node folder? Or should I create another folder under the React app consisting of the js files for different views?
Thank you. I would gladly to answer questions if my post lacks information.
There's an interesting hand to it.
App
__|__
| |
Frontend Backend -->Database(MongoDB)
I think this scanty tree should do justice to it.
I'd suggest you use MERN
MongoDB
ExpressJs
ReactJs
Nodejs
Your apps need to be on different servers. This method is effective and secure.
Since you already know React, Express, Nodejs... You can check out this link (by cleverprogrammer)to see some MERN projects and how each part of the app communicates with one another seamlessly.
https://youtu.be/ktjafK4SgWM

Can I merge a node server with the polymer init starter-kit application?

I have my fledgling polymer application running. I would like to load and save a file. I would like to do that by using the ajax element. But what happens on the server side?
I have made nodejs express applications before. I could make a separate server for the client to talk to. But there are at least two other options:
Take the front-end material supplied by the polymer start-kit and put it in the /public directory of an express application.
Put express routes in my polymer starter-kit application.
I am inclined to bring Express into the existing start-kit application. But maybe someone else has already tried this, and can tell me what I will run into?
I looked at the starter-kit code a little bit. Apparently, it uses the spdy package and not the http package, but I can work with that. That's as far as I have gotten. Any advice?
Regards, Rick
I talked to the guys on the Polymer slack channel. Thanks, milesje. polymer serve is just supposed to be a light server to serve static files. If you want to do something more substantial, you should create your own server (nodejs or something else) and put your Polymer files in the public/ directory.
Hope this saves someone else some time.

Bigger projects Node.js and RESTful API

I'm looking into node.js which really seem like a pretty nice environment. I've worked with a lot of different Technologies and for server, mainly php and Java (jsp), but dabbled in som RoR and Python.
I find node.js really easy to get up and running and it feels quite natural to work with, and I found some good entry level tutorials.
I just am missing some more intermediate resources. For example when creating bigger frameworks or api's how would you structure or architect it. I set up some smaller api's to try it out where it would go something like this:
I've made use of the Express framework to create a http server, listen to a port, set up an express object and bound some requests.
However these have been quite small, and the purpose has been learning, if I think about scaling up the size of the API for production, perhaps wanting to do other stuff like serve web-pages as well. I find it hard to see how the architecture would look.
It's vague as I am still new to node.js but I'm mainly thinking about things like if you typically keep all api in one file or if there are good ways to split it up into modules? And if anyone know any resource talking a bit more about how to design the architecture when working in node.js
Sorry for the vague question and thanks for reading.
In my opinion, Express is the good way to go if you want to build complex or big APIs.
It is among others easily testable (for instance with Mocha or Jasmine) and customizable, especially thanks to its middlewares.
For the directory structure, what I usually use is (at least) the following:
app.js : the main entrypoint. Will create the express application, indicate which controller to use for every route prefix, and assign the middlewares. Example from a previous project
controllers : will contain the controllers, the functions which will handle the requests, in the same style as in standard MVC frameworks (e.g. UserController, ...). Each controller would create an express Router object and export it. Inside the controllers, individual handlers are in charge of individual API requests, such as /api/users/list. It would use some library to access your data (e.g. Mongoose for MongoDB), and would then send the response to the client. Example (UserController.js)
models : will contain the models with all their attributes and methods. In my case, it would be the Mongoose models. Example (Song.js)
middlewares : will contain the various middlewares of the project. A practical example would be a middleware checking for an access token in the incoming request, and returning a 403 HTTP error if not. Example (AuthMiddleware.js)
helpers : various helpers
tests : unit tests of you API
This could be the minimal directory organization. On top of that, you may want to use a templating engine such as EJS to serve webpage. Take a look at « Use EJS to template your node application ».
This is only to give you an overview of what an express directory structure could look like, but there are of course plenty (better?) other possibilities. Hope that gives you a quick and useful insight :)

Node.JS Express structuring a big app

I am interested in creating a CRUD Rest API using Node.js together with express. I was wondering if their was a structuring standard of some sort or MVC framework which is used in the industry to structure my code create models etc...
I know that I should structure the different models into different npm projects but how to structure a single project is what I am looking for...
Also I am very interested in using a mediator design pattern to decrease coupling between the different modules.
Any examples/blogs/gits/books will help
Thanks
I did a screencast on this topic in which I propose an app structure that has served me well for APIs and projects with UIs. Do I claim this is gospel and then end-all-be-all? No. But it has served me well.
In short it looks something like this:
app_root
app
routes
- route handlers go here
models
- if you use models
commands
- if you use commands
middleware
- middlewarez
config
application.js - the stuff that bootstraps your application. Reusable in contexts other than your server (thing testing)
routes.js - All your route mappings in one place
test
test_helper.js - Bootstrap your testing (require config/application.js, etc)
models
- tests for your models (follow suit for other things under app)
server.js - starts up your webserver
I put this empty app structure up on GitHub here.
You could use Google's AngularJS + Node.JS where the angular frameworkwork separates view and controller. Look into Angular site for example and tutorial.

Why would one want to use Express instead of AngularJS?

I understand that Express resides on the server and Angular resides on the client but, as far as I know, Angular can do everything that Express can do which is
routing
interacting with the database
It kind of seems like maybe Express is needed in order for an AngularJS app to be served by Node.js but I'm not sure.
So what are the benefits to adding Express to an AngularJS app?
There are things which should be done server side (i.e. Express, not Angular), most notably user input validation - Angular, as it's client side, can be tampered.
Also, if you'll ever want to offer access type other than web app (i.e. mobile app), you'll probably need an API anyway - Express can do this, Angular don't.
Finally, database access - usually Angular app will need to connect to some kind of backend to perform CRUD operations. You'll either go with hosted DB like Firebase, or you'll end up using your own database. Latter scenario is more popular and you'll need Express (or similar) for that.
Express and AngularJS do not mutually exclude one another, they serve different purpose - in fact it's perfectly fine to use both - express for all your serverside logic, and Angular for client side logic.
Express can be used to host the APIs for AngularJS's service/factory to consume. You can consider AngularJS as MVC and the API on Express as SOA.
There is lot of stuff that one wants to control from server. And that is the place where the server side frameworks come into picture.
An web app is not just some html pages linked together. There are lot of other things that needs to be implemented
Model validation.
Keeping model consistent. Remember multiple users can access the same model at any give time and even change it.
Controlling resource access.
Triggering workflows.
Business Logic.
and other such thing require a server framework. So as mentioned earlier the client side frameworks like AngularJS complement server side frameworks.

Resources