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