app.get 不适用于基本路由

app.get is not working for base route

本文关键字:路由 适用于 get 不适用 app      更新时间:2023-09-26

我是node的新手.js我尝试通过这项技术创建一个新项目。我使用快速框架,但在开始时我遇到了一点麻烦。我已经用解决方法解决了这个麻烦,但我对下一个行为有一个问题:我的应用.js

var express = require('express')
  , routes = require('./routes/index')
  , routes = require('./routes/login');
var app = module.exports = express.createServer(); 
console.log(app.env);
// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  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.use( function(req, res, next) {
    if (req.url == '/') {
        res.render('index', { title: 'Express!' })
    } else {
        next();
    }
});
//app.get('/', routes.index);
app.get('/login', routes.login);

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

在一个块中 //路由,您可以看到 app.use 和 app.get如果我尝试使用 app.get 而不是 app.use,则会出现错误"无法获取/"。我试图将索引.html文件放入我的公用文件夹。但是每次我都得到这个文件,而不是 index.js 的渲染。

app.get('/login', routes.login(; - 工作正常,但"/"路由有问题。我不想让我的代码处于这种状态,请帮助我理解这种行为。

提前谢谢。

就像用户 PA 一样。 提到,您的代码从未找到/ URL 的原因是因为您正在重新声明您的 routes 变量:

var express = require('express')
  , routes = require('./routes/index')  // first declaration of 'routes'
  , routes = require('./routes/login'); // re-declaration of 'routes'

这使得你的代码无法访问你的第一个路由声明(指向/index 的声明(,这就是为什么你得到错误"无法获取/",因为你的 routes 变量只指向 ./routes/login。

有几种方法可以解决此问题来清理代码:

1. 为不同的路线分配不同的变量:

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

0R -

2. 在路由文件中放置多个函数:

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// in your routes/index file
exports.index = function(req, res){
   res.render('index', { title: 'Index Page' });
};
exports.login = function(req, res){
   res.render('login', { title: 'Login Page' });
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
// in your app.js file
// import your 'routes' functions from your 'routes' file 
var express = require('express')
      , routes = require('./routes/')  

// now use your routes functions like this:
app.get('/', routes.index);
app.get('/login', routes.login);

-或-

在大型应用程序中,或者为了更好的代码可维护性,您可能希望将不同的路由函数分解到不同的文件中(而不是将所有路由函数放在一个文件中,如上例所示(,因此使用显式默认设置作为示例,它们将其user函数和index函数放在 routes 文件夹中,如下所示:

routes /
   user.js
   index.js

然后,他们像这样设置他们的应用程序:

var routes = require('./routes');
var user = require('./routes/user');

并像这样调用这些函数:

app.get('/', routes.index); // calls the "index" function inside the routes/index.js
app.get('/users', user.list); // calls the "list" function inside the routes/user.js file

希望这有帮助。

快速提示:app.use()用于创建中间件,中间件是一个将在应用程序中的每个请求上调用的函数,使您(开发人员(可以访问请求对象req和响应对象res,并具有更改或以某种方式增强应用程序的能力。"在请求中间操作"的能力是一个强大的功能,它之所以在你的原始示例中为你工作,是因为你的app.use()中间件在调用/请求时被调用,即使你的应用找不到/,当你重新声明routes变量时,它丢失了, 您仍在向 / 发出请求,您的 app.use(( 能够看到该请求(因为中间件在每个请求上都会被调用,即使是"坏"的请求(,所以您的中间件仍然看到 [无效] /请求并像这样操作:

// This middleware will get called on every request, 
// even on the invalid request for '/'
app.use( function(req, res, next) {
    // this line of code will see that the 
    // request is for "/" and fire
    if (req.url == '/') {
        // the page now appears to render, because you are forcing the
        // res.render code inside of your middleware, which isn't always
        // recommended, but it is working for you in this example
        // because its the only place in your example that can do anything
        // when the '/' request is made
        res.render('index', { title: 'Express!' })
    } else {
        // this is common middleware design pattern, the next()
        // function tells the express framework to "move on to the next function"
        // in the middleware stack
        next();
    }
});