问题
I am new to nodejs and a little confused about the distinction between the server and client side html pages. My goal is to build an e-commerce web store for practice. The stack that I want to try is NodeJS + Express + MongoDB + AngularJS. The basic structure I have right now is as below.
shoppingMall
..bin
..data
..node_modules
..public
....images
....javascripts
....stylesheets
..routes
....index.js
....users.js
..views
....index.jade
....layout.jade
..app.js
..package.json
Here is my logic. The files inside views are html pages that are rendered from server. Javascript files inside public/javascripts/ are rendered on client. I have to include AngularJS inside layout.jade, and any client code related to the index page should go to public/javascripts/index.js and I must include this file from index.jade. Then, the html page is rendered from server using a jade template engine, and any further user interaction is done from client. Any server-side logic related to index.jade must go to routes/index.js and the code that lives inside this file will not be shown to client.
Q1. Is my logic correct?
Q2. Assuming I am trying to keep it up as an MVC structure, which parts correspond to M, V, C in this case?
回答1:
Q1. Is my logic correct?
My experience says your logic is incorrect. Because there isn't any need for a server side templating engine (in your case, jade) when we are using Angularjs. Angular itself is capable for rendering dynamic data.
What you could do here is use node and express for building API end points for all the functionalities of your shopping cart and then write all the views (both frontend and backend) in Angular.
Q2. Assuming I am trying to keep it up as an MVC structure, which parts correspond to M, V, C in this case?
In angular View is the html page with directives and filters applied. So that represents the UI side of the application. Controllers are the brain for the views.
Views get data from controllers to display. And controllers often fetch data from APIs using services or factories. Controller use an object called $scope to pass data into view. In angular world you can consider this $scope object as the view model. it glues both the view and controller.
So in your example,
/public
/javascripts
/item
..item.html // view
..item.js // controller (might include the services for item api calls or write a seperate js file for them)
来源:https://stackoverflow.com/questions/27750932/nodejs-expressjs-server-side-vs-client-side-html-rendering