Node. js notes (5) Talk about routing and node. js notes about Routing

Source: Internet
Author: User

Node. js notes (5) Talk about routing and node. js notes about Routing

The routing usage problem is also mentioned in previous notes, but there is always a "passive" feeling that you must enter an address in the browser to access the corresponding url. We hope to click a button or link to go to another page. We finally finished this yesterday.

There are three main files used,
App. js: Define routing rules
Register. js: controller to implement routing Logic
Register. ejs: Implements page rendering,

Main Logic: The ejs template triggers the route table event, which is handled by the Controller.

Core methods:
res.render();
Some articles on the Internet use res. redirect (), which is useless here.
Directory structure: You can use express commands to directly generate a directory structure.

First, define routing rules in app. js,

app.get('/apple',register.changed);

Call register when you receive the '/apple' route request. changed method. The route name is optional. The real page name (rendering template) is in register. changed method, that is, register. js File

Logic Control in register. js:

exports.changed = function(req,res){    res.render('change2',{title:'Change2'});};

Register. ejs renders the page. The key point is to define a jump link, which will be intercepted and processed by routing rules,

<P> <a href = "apple"> try to jump </a> </p>

Before jump:

After jump:

Source code:
Http://pan.baidu.com/s/1dD8LeUl

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.