Node Express中的Jade URL路由 [英] Jade URL Routing in Node Express

查看:90
本文介绍了Node Express中的Jade URL路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jade构建Node Express应用程序,并且对于如何将视图路由到浏览器将发出的特定请求感到困惑.我了解,为了使URL在浏览器中正常工作,我们需要使用Node的路由.但是,从网上看,我发现Express拥有自己的路由器.

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. 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.

我用PHPStorm启动了我的项目,index.jade将被加载...但是我该如何加载其他的?这是我现有的代码:

I used PHPStorm to start up my project, and the index.jade will load... but how do I load the others? Here is my existing code:

var express = require('express'), routes = require('./routes'), http = require('http'), path = require('path');

var app = express();

app.configure(function ()
{
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser('your secret here'));
    app.use(express.session());
    app.use(app.router);
    app.use(require('less-middleware')({ src:__dirname + '/public' }));
    app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function ()
{
    app.use(express.errorHandler());
});

app.get('/', routes.index);

http.createServer(app).listen(app.get('port'), function ()
{
    console.log("Express server listening on port " + app.get('port'));
});

路由应用程序的最基本方法是什么,在哪里可以找到有关此主题的更多文档?

What is the most basic way to route my application, and where can I find more extensive documentation on this topic?

谢谢.

推荐答案

我了解到,为了使URL在浏览器中正常工作, 我们需要使用Node的路由;但是,从网上看 我发现Express有自己的路由器.

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本身不提供对路由"的支持,但Express则提供.您可以使用以下语法在Express中构建路由:

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]);

因此,您的路由 app.get('/',routes.index)将使用route.index函数处理对URL路径/的HTTP GET请求. Express会自动将请求和响应对象传递给您的处理程序.

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);

您可以在此处找到有关此内容的更多信息 http://expressjs.com/api.html# app.param

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

我正在使用Jade构建Node Express应用程序,而我 对于如何将我的观点传递给特定的人感到困惑 请求浏览器发出.

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.

一旦调用了路由处理程序,例如说(routes.userList),您可以在userList中调用res.render()方法来呈现所需的Jade文件.例如:

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}] });

有关更多信息,请参见此处: http://expressjs.com/api.html#res.渲染

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

这篇关于Node Express中的Jade URL路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆