Nodejs How much do you know about Web development? An article to let you know Nodejs development web

Source: Internet
Author: User
This article gives us an idea of Nodejs's process of web development, and what you want to know here is that the HTML page is dynamically generated by the server. Let's look at the article below.

one, node. js and PHP, Perl, ASP, JSP purposes are to implement Dynamic Web page , that is, the server dynamically generated HTML pages. This is done because the extensibility of static HTML is very limited and does not interact effectively with the user. (Tutorial recommended: node. JS Chinese Reference manual)

Software engineering breaks down into layers: models, views, and controllers.

    1. A model is an implementation of an object and its data structure, typically containing database operations.

    2. Views represent the user interface, which is usually the organization of HTML in a Web site.

    3. The controller handles user requests and data flows, complex models, and passes the output to the view.

Preparatory work

1. Using the HTTP Module

POST request:

var http = require (' http '); var querystring = require (' querystring '); var server = Http.createserver (function (req, res) {VA R post = '; Req.on (' Data ', function (chunk) {post + = chunk;}); Req.on (' End ', function () {post = Querystring.parse (post);        Res.write (post.title);        Res.write (post.text);        Res.end ();}); }). Listen (3000);

So compared to PHP, to use NODEJS with the HTTP module directly to develop the site, must manually implement everything, want to learn node. JS students can go to topic.alibabacloud.com node. JS Video Tutorial Section

Second, express framework

The only Web development framework recommended by NODEJS

In addition to providing a higher-level interface for HTTP modules, many features are implemented, including:

    • -Routing control

    • -Die Analysis support

    • -Dynamic View

    • -User session

    • -CSRF Protection

    • -Static File Service

    • -Error Controller

    • -Access logs

    • -Cache

    • -Plug-in support

Quick Start

1. Install Express

$ NPM Install-g Express

2. Establishment of the project

$ express-t ejs microblog$ CD microblog && npm install

3. Start the server

$ node App.js

Third, routing control

1. Working principle

    • Accessing the Http://localhost:12138 browser sends a request to the server

    • The app parses the path of the request, invoking the appropriate logic

    • One line in App.js is App.get ('/', routes.index), which specifies that a GET request with a path of/is handled by the Routes.index function

    • Routes.index Call the View template index, passing the title variable via res.render (' index ', {title: ' Express '})

    • The final view template generates an HTML page, which is returned to the browser

    • After the browser receives the content, the analysis discovers to obtain the/STYLESHEETS/STYLE.CSS, therefore will again the server sends the request.

    • There is no routing rule in app.js that refers to/stylesheets/style.css, but the app is configured with a static file server through App.use (express.static (__dirname + '/public ')), so/ Stylesheets/style.css is directed to the file Public/stylesheets/style.css in the subdirectory of the directory where App.js is located, returning the style content to the client

2. Create a routing rule

Open App.js, add a row after the existing routing rule app.get ('/', Routes.index)

App.get ('/hello ', Routes.hello); change routes/index.js, add Hello function: Exports.index = function (req, res) {      Res.render (' Index ', {title: ' Express '}); Exports.hello = function (req, res) {  res.send (' The time is ' + new Date (). toString ());};

REST-style routing rules

Four, the mold version of the engine

1. What is a template engine

Template engine is a tool that generates HTML from a page template according to certain rules

The function of the template engine is to combine the page template and the data to be displayed to generate an HTML page.

It can run on the server side can run on the client

The mainstream is still run by the server template engine

In the MVC architecture, the template engine is included on the server side, and after the controller gets the user request, the template engine is fetched from the model data call.

The template engine takes data and page templates as input, generates HTML pages, and returns them to the controller

Handed back to the client by the controller

Location of the template engine in MVC:

2. Using the template engine Ejs

Ejs Label system is very simple, 3 label:

    1. -<% code%>: JS codes

    2. -<%= code%>: Displays the contents of the replaced HTML special characters

    3. -<%-code%>: Display original HTML content

These are all the content of Nodejs on Web development (more details to topic.alibabacloud.comnode.js Chinese reference manual), there are questions can be left in the comments below.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.