concept view in category node

This is an excerpt from Manning's book Get Programming with Node.js.
In this lesson, you learned how to serve individual files. First, you added the fs module to your application to look for HTML files in your views folder. Then you extended that functionality to application assets. You also learned how to apply your routing system to its own module and selectively register routes from your main application file. In unit 2, I talk about how you can use the application structure provided by Express.js, a Node.js web framework.
The views are client-facing and could make or break my user’s experience with the application. I’ll use a similar template for each page to reduce complexity in this application. The top of each HTML page should have some HTML layout, a head, a link to my soon-to-be-built custom stylesheet, and navigation. The home page for the Confetti Cuisine site will look like figure 7.1, with links to my three views in the top-left corner.

This is an excerpt from Manning's book Getting MEAN with Mongo, Express, Angular, and Node.js 2ED.
Two main steps are accomplished in this chapter, so two versions of the source code are available. The first version contains all the data in the views and represents the application as it stands at the end of section 4.4. This code is available from the chapter-04-views branch on GitHub.
At the moment, all the content and data are held in the views. This arrangement is perfect for testing stuff and moving things around, but you need to move forward. A goal of the MVC architecture is to have views without content or data. The views should be fed data that they present to the end user while being agnostic about the data they’re fed. The views need a data structure, but what’s inside the data doesn’t matter to the view itself.
Consider the MVC architecture: the model holds the data; the controller processes the data; and, finally, the view renders the processed data. You’re not dealing with the model yet; you’ll do that starting in chapter 5. For now, you’re working with the views and controllers.
To make the views smarter and do what they’re intended to do, you need to take the data and content out of the views and put it in the controllers. Figure 4.14 illustrates the data flow in an MVC architecture and the changes you want to make.
Figure 4.14. How the data should flow in an MVC pattern, from the model through the controller to the view. At this point in the prototype, your data is in the view, but you want to move it a step back into the controller.
![]()
Making these changes now allows you to finalize the views so that you’re ready for the next step. As a bonus, you’ll start thinking about how the processed data should look in the controllers. Rather than starting with a data structure, start with the ideal front end and slowly reverse-engineer the data back through the MVC steps as your understanding of the requirements solidifies.
How are you going to do these things? Starting with the homepage, you’ll take every piece of content out of the Pug view. You’ll update the Pug file to contain variables in place of the content and put the content as variables in the controller. Then the controller can pass these values into the view. The result should look the same in the browser, and users shouldn’t be able to spot a difference. The roles of the various parts and the movement and use of data are shown in figure 4.15.
Figure 4.15. When the controller specifies the data, it passes the data to the view as variables; the view uses that data to generate the final HTML that’s delivered to the user.
![]()
At the end of this stage, the data is still hardcoded, but in the controllers instead of the views. The views are now smarter and able to accept and display whatever data is sent to them (provided that the data is in the correct format, of course).
This controller is already sending one piece of data to the view. Remember that the second parameter in the render function is a JavaScript object containing the data to send to the view. Here, the homelist controller sends the data object { title: 'Home' } to the view. This object is being used by the layout file to put the string Home in the HTML <title>, which isn’t necessarily the best choice of text.
Now that the controller is passing these pieces of data to the view, you can update the view to reference them in place of the hardcoded content. Nested data items like these are referenced using dot syntax, as you do when getting data out of objects in JavaScript. To reference the page header strapline in the locations-list.pug view, use pageHeader.strapline. The following code snippet shows the page header section of the view (modifications in bold):
.row.banner .col-12 h1= pageHeader.title #1 small #{pageHeader.strapline} #2The code outputs pageHeader.title and pageHeader.strapline in the relevant places in the view. See the sidebar “Referencing data in Pug templates” for more details.

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.
React isn’t a framework like Angular or Ember. It only provides the code you use to write your view components. It can fit easily into a Model-View-Controller (MVC) style architecture as the view. But there’s a recommended way to build complex React apps, which is covered throughout the book.
Express— Enables simple server-side routing for rendering the view.
Listing 2.9. Action creators for recipes and featured data—src/action-creators.es6
export const GET_RECIPES = 'GET_RECIPES'; #1 export const GET_FEATURED_RECIPE = 'GET_FEATURED_RECIPE'; #1 export function fetchRecipes() { #2 return dispatch => { return fetch('http://localhost:3000/recipes', { #3 method: 'GET' }).then((response) => { return response.json().then((data) => { #4 return dispatch({ #5 type: GET_RECIPES, #6 data: data.recipes #7 }); }); }) } } export function fetchFeaturedRecipe() { #8 return dispatch => { return fetch('http://localhost:3000/featured', { #3 method: 'GET' }).then((response) => { return response.json().then((data) => { #4 return dispatch({ #9 type: GET_FEATURED_RECIPE, #6 data: data.recipe #10 }); }); }) } } export function getHomePageData() { #11 return (dispatch, getState) => { return Promise.all([ dispatch(fetchFeaturedRecipe()), dispatch(fetchRecipes()) ]) } } #1 Best practice is to create constants for all your actions so action creators (functions listed here) and reducers can use them. Then you won’t have discrepancies between strings. #2 fetchRecipes, the first action creator, handles logic for making a request to the server for recipes data. #3 Implements fetch API for making GET request to appropriate endpoint. #4 On a successful response, get JSON from the response—using a promise to get JSON response is standard with the fetch API. #5 Dispatch action. #6 Type is the only required property of every action, set it using string constants declared at the top of the module. #7 Attach JSON data to action payload on a property called data. #8 The second action creator handles the logic for requesting the featured recipe from the server. #9 Dispatch action. #10 Attach JSON data to action payload on a property called data. #11 This action creator composes the other two action creators—making it easier for view and server to request related data. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":71},{\"line\":0,\"ch\":73}],[{\"line\":1,\"ch\":71},{\"line\":1,\"ch\":73}],[{\"line\":26,\"ch\":71},{\"line\":26,\"ch\":73}],[{\"line\":33,\"ch\":71},{\"line\":33,\"ch\":73}],[{\"line\":42,\"ch\":0},{\"line\":42,\"ch\":2}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":2}],[{\"line\":52,\"ch\":0},{\"line\":52,\"ch\":2}],[{\"line\":0,\"ch\":71},{\"line\":0,\"ch\":73}],[{\"line\":1,\"ch\":71},{\"line\":1,\"ch\":73}],[{\"line\":26,\"ch\":71},{\"line\":26,\"ch\":73}],[{\"line\":33,\"ch\":71},{\"line\":33,\"ch\":73}],[{\"line\":42,\"ch\":0},{\"line\":42,\"ch\":2}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":2}],[{\"line\":52,\"ch\":0},{\"line\":52,\"ch\":2}],[{\"line\":3,\"ch\":71},{\"line\":3,\"ch\":73}],[{\"line\":43,\"ch\":0},{\"line\":43,\"ch\":2}],[{\"line\":5,\"ch\":71},{\"line\":5,\"ch\":73}],[{\"line\":20,\"ch\":71},{\"line\":20,\"ch\":73}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":8,\"ch\":71},{\"line\":8,\"ch\":73}],[{\"line\":23,\"ch\":71},{\"line\":23,\"ch\":73}],[{\"line\":45,\"ch\":0},{\"line\":45,\"ch\":2}],[{\"line\":9,\"ch\":71},{\"line\":9,\"ch\":73}],[{\"line\":46,\"ch\":0},{\"line\":46,\"ch\":2}],[{\"line\":10,\"ch\":71},{\"line\":10,\"ch\":73}],[{\"line\":25,\"ch\":71},{\"line\":25,\"ch\":73}],[{\"line\":47,\"ch\":0},{\"line\":47,\"ch\":2}],[{\"line\":11,\"ch\":71},{\"line\":11,\"ch\":73}],[{\"line\":48,\"ch\":0},{\"line\":48,\"ch\":2}],[{\"line\":18,\"ch\":71},{\"line\":18,\"ch\":73}],[{\"line\":49,\"ch\":0},{\"line\":49,\"ch\":2}],[{\"line\":5,\"ch\":71},{\"line\":5,\"ch\":73}],[{\"line\":20,\"ch\":71},{\"line\":20,\"ch\":73}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":8,\"ch\":71},{\"line\":8,\"ch\":73}],[{\"line\":23,\"ch\":71},{\"line\":23,\"ch\":73}],[{\"line\":45,\"ch\":0},{\"line\":45,\"ch\":2}],[{\"line\":24,\"ch\":71},{\"line\":24,\"ch\":73}],[{\"line\":50,\"ch\":0},{\"line\":50,\"ch\":2}],[{\"line\":10,\"ch\":71},{\"line\":10,\"ch\":73}],[{\"line\":25,\"ch\":71},{\"line\":25,\"ch\":73}],[{\"line\":47,\"ch\":0},{\"line\":47,\"ch\":2}],[{\"line\":26,\"ch\":71},{\"line\":26,\"ch\":74}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":33,\"ch\":71},{\"line\":33,\"ch\":74}],[{\"line\":52,\"ch\":0},{\"line\":52,\"ch\":3}]]"} !@%STYLE%@!

This is an excerpt from Manning's book Express in Action: Writing, building, and testing Node.js applications.
Views —Views allow you to dynamically render HTML. This both allows you to change the HTML on the fly and to write the HTML in other languages.
The next listing shows what it looks like to set up views.
This should look exactly like HTML to you, but for the one weird bit inside the body tag. EJS is a superset of HTML, so everything that’s valid HTML is valid EJS. But EJS also adds a few new features, like variable interpolation. <%= message %> will interpolate a variable called message, which you’ll pass when you render the view from Express. Here’s what that looks like.

This is an excerpt from Manning's book hapi.js in Action.