在Express-js中使用路由 [英] Using routes in Express-js

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

问题描述

所以我开始使用Node.js.我看到了Ryan Dahl在 Nodejs.org 上的视频,并听说他推荐了Express-js的网站。

So I'm starting to use Node.js. I saw the video with Ryan Dahl on Nodejs.org and heard he recommended Express-js for websites.

我下载了最新版本的Express,并开始编写代码。我有一个完全成熟的静态查看在/,但一旦我尝试发送参数,我得到这样的错误:

I downloaded the latest version of Express, and began to code. I have a fully fledged static view up on /, but as soon as I try sending parameters, I get errors like this:

Cannot GET /wiki

我尝试按照 expressjs.com ,但是使用路线的方式在最新版本中有所改变,这使得该指南无法使用。

I tried following the guide on expressjs.com but the way one uses routes has changed in the latest version, which makes the guide unusable.

指南:

app.get('/users/:id?', function(req, res, next){
    var id = req.params.id;
    if (id) {
        // do something
    } else {
        next();
    }
});

由Express生成:

Generated by Express:

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

当我尝试添加其他路线时,出现问题。

My problem arises when I try and add another route.

app.get('/wiki', routes.wiki_show);

我已经尝试了一大堆方法,但是我不断得到 GET / wiki (404)错误。

I've tried a bunch of approaches, but I keep getting the Cannot GET /wiki (404) error.

routes / index.js如下所示:

routes/index.js looks like this:

exports.index = function(req, res) {
    res.render('index', { title: 'Test', articles: articles, current_article: current_article, sections: sections })
};

我在这里做的唯一的事情是添加一些参数(同一个文件中的数组),这个我工作。但是当我复制内容并将<。c $ c> exports.index 更改为 exports.wiki 导出。 wiki_show 我仍然得到不能GET / wiki 错误。

The only thing I did there was add some parameters (arrays in the same file) and this i working. But when I copy the contents and change exports.index to exports.wiki or exports.wiki_show I still get the Cannot GET /wiki error.

任何人都可以解释我在这里缺少什么? - 谢谢。

Can anyone explain to me what I'm missing here? - Thanks.

推荐答案

所以,在我创建了我的问题之后,我在右边找到了相关的列表: a href =https://stackoverflow.com/questions/4602212/organize-routes-in-node-js>组织Node.js中的路由。

So, after I created my question, I got this related list on the right with a similar issue: Organize routes in Node.js.

该链接中的答案与 GitHub快速回购相关联,并建议请查看路线分离示例。

The answer in that post linked to the Express repo on GitHub and suggests to look at the 'route-separation' example.

这有助于我更改代码,现在我可以使用它了。 - 感谢您的意见。

This helped me change my code, and I now have it working. - Thanks for your comments.

我的实现最终看起来像这样;

My implementation ended up looking like this;

我需要我的路线app.js:

I require my routes in the app.js:

var express = require('express')
  , site = require('./site')
  , wiki = require('./wiki');

我添加了这样的路线:

app.get('/', site.index);
app.get('/wiki/:id', wiki.show);
app.get('/wiki/:id/edit', wiki.edit);

我的应用程序根目录中有两个名为wiki.js和site.js的文件,其中包含:

I have two files called wiki.js and site.js in the root of my app, containing this:

exports.edit = function(req, res) {

    var wiki_entry = req.params.id;

    res.render('wiki/edit', {
        title: 'Editing Wiki',
        wiki: wiki_entry
    })
}

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

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