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

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

问题描述

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

我下载了最新版本的 Express,并开始编码.我在/上有一个完全成熟的静态视图,但是一旦我尝试发送参数,就会出现如下错误:

无法获取/wiki

我尝试按照 expressjs.com 上的指南进行操作,但是在最新版本中使用路由的方式发生了变化,使指南无法使用.

指南:

app.get('/users/:id?', function(req, res, next){var id = req.params.id;如果(身份证){//做点什么} 别的 {下一个();}});

由 Express 生成:

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

当我尝试添加另一条路线时,我的问题出现了.

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

我尝试了很多方法,但我不断收到 Cannot GET/wiki (404) 错误.

routes/index.js 看起来像这样:

exports.index = function(req, res) {res.render('index', { title: 'Test', 文章: 文章, current_article: current_article, Sections: Sections })};

我在那里做的唯一一件事就是添加一些参数(同一文件中的数组),这就是我的工作.但是当我复制内容并将 exports.index 更改为 exports.wikiexports.wiki_show 时,我仍然得到 Cannot GET/wiki 错误.

谁能向我解释我在这里遗漏了什么?- 谢谢.

解决方案

所以,在我创建我的问题之后,我在右边得到了这个相关列表,并有一个类似的问题:在 Node.js 中组织路由.

该帖子中的答案链接到 GitHub 上的 Express repo 并建议查看路由分离"示例.>

这帮助我更改了代码,现在我可以使用它了.- 感谢您的评论.

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

我需要在 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 的文件,其中包含:

exports.edit = function(req, res) {var wiki_entry = req.params.id;res.render('维基/编辑', {title: '编辑维基',维基:wiki_entry})}

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.

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

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.

Guide:

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

Generated by Express:

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

My problem arises when I try and add another route.

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

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

routes/index.js looks like this:

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

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.

解决方案

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

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;

I require my routes in the app.js:

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

And I add my routes like this:

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

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天全站免登陆