使用express.js路由 - 无法GET错误 [英] Routing with express.js - Cannot GET Error

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

问题描述

第一次使用 express.js ,我被困在添加我的第一条路线。



我的路线在 app.js 中定义,如下所示:

 应用程序.get('/ user /:id / photos',function(req,res){
res.send('user'+ req.params.id);
});

但是卷曲到 http:// localhost:3000 / user / 1 /照片只是导致无法GET错误。



包含在express中的索引文件似乎工作正常。 p>

这是我的快递 app.js 文件:

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

var app = module.exports = express.createServer();

//配置
app.configure(function(){
app.set('views',__dirname +'/ views');
app.set ('view engine','jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app .router);
app.use(express.static(__ dirname +'/ public'));
});

app.configure('development',function(){
app.use(express.errorHandler({
dumpExceptions:true,
showStack:true
}));
});

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

//路由
app.get('/',routes.index);
app.get('/ test',routes.test);
app.get('/ user /:id / photos',function(req,res){
res.send('user'+ req.params.id);
}) ;

app.listen(3000);
console.log(Express服务器监听%s模式下的端口%d,app.address()。port,app.settings.env);


解决方案

尝试更改从最具体到最不具体的路线。路线按顺序进行匹配。如果它首先匹配'/'路由,它将传递给 routes.index ,永远不会有机会路由到 / user /:id / photos 函数。



所以,而不是:

  app.get('/',routes.index); 
....
app.get('/ user /:id / photos',function(req,res){

尝试:

  app.get('/ user /:id / photos ',function(req,res){
....
app.get('/',routes.index);

作为附注,我认为 / user / photos /:id 似乎更好,但我尝试了 / something /:id / somethingelse 在我的应用程序,它工作正常。


Working with express.js for the first time, and I am stuck on adding my first route.

My route is defined in app.js like so:

app.get('/user/:id/photos', function(req,res){
    res.send('user' + req.params.id);
});

However curling to http://localhost:3000/user/1/photos just results in a "cannot GET" error.

The index file that was included with express seems to work just fine though.

This is my express app.js file:

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

var app = module.exports = express.createServer();

// Configuration
app.configure(function() {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});

app.configure('development', function() {
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});

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

// Routes
app.get('/', routes.index);
app.get('/test', routes.test);
app.get('/user/:id/photos', function(req, res) {
    res.send('user' + req.params.id);
});

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

解决方案

Try changing the routes from most specific to least specific. Routes are matched in order. If it matches on the '/' route first, it will pass to routes.index, and never get a chance to router to the /user/:id/photos function.

So, instead of:

app.get('/', routes.index);
....
app.get('/user/:id/photos', function(req,res){

Try:

app.get('/user/:id/photos', function(req,res){
....
app.get('/', routes.index);

As a side note, I think /user/photos/:id seems better but I tried out /something/:id/somethingelse in my app, and it worked fine.

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

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