Jade URL Routing in Node Express

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 07:15:53

I understand that in order to get URLs to work in the browser, we need to use Node's routes; however, from looking online, I have discovered that Express has its own router.

Node.js per-se does not provide support for "routes", but Express does. You build your routes in Express using the following syntax:

app.[verb]('[url-path]', [handler]);

So your route app.get('/', routes.index) will process HTTP GET request to URL path / with the routes.index function. Express will automatically pass a request and response objects to your handler.

You can add more routes like this:

app.get('/users', routes.userList);
app.get('/user/:id', routes.userInfoView);
app.post('/user/:id', routes.userInfoSave);

You can find more information about this here http://expressjs.com/api.html#app.param

I am building a Node Express application using Jade, and I am confused about how to route my views to the specific requests the browser will make.

Once a route handler is invoked, say (routes.userList) you can call res.render() method inside userList to render the Jade file that you want. For example:

res.render('user_list', 
    { users: [{name: "user1", age: 10}, {name: "user2", age: 20}] });

See here for more information: http://expressjs.com/api.html#res.render

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!