javascript - 无法获取路由 express /nodejs

标签 javascript node.js express

我正在学习更多关于 API 的知识,所以我决定为啤酒/啤酒厂构建一个简单的 API,它运行良好,然后突然没有路由可用。我通过创建一条纯粹用于测试此问题的新路线对其进行了测试,并且效果很好。因此,我简化了我的其他路线,从字面上看只是使用 res.send() 将消息发送回 DOM,以查看切断 DB 连接是否会帮助我查明问题所在。

此测试路由通过向“hello!”的 DOM 发送消息来按预期工作。 (/app/routes/test_routes.js):

module.exports = function(app, db) {
  app.get('/test', (req, res) => {
    res.send("hello!");
  });
};

但是,尽管功能相同(/app/routes/brewery_routes.js),但这根本不起作用:

module.exports = function(app, db) {
  app.get('/brewery', (req, res) => {
    res.send("hello!");
  });
};

这是将我所有路线拉入 (/app/routes/index.js) 的路线索引:

const beerRoutes = require('./beer_routes');
module.exports = function(app, db) {
  beerRoutes(app, db);
};

const breweryRoutes = require('./brewery_routes');
module.exports = function(app, db) {
  breweryRoutes(app, db);
};

const cityRoutes = require('./city_routes');
module.exports = function(app, db) {
  cityRoutes(app, db);
};

const testRoutes = require('./test_routes');
module.exports = function(app, db) {
  testRoutes(app, db);
};

这是我处理所有路由的服务器文件(/server.js):

const express     = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser  = require('body-parser');
const app         = express();
const db          = require('./config/db');

const PORT = 8000;

app.use(bodyParser.urlencoded({ extended: true }));

MongoClient.connect(db.url,(err, database) =>{

  if (err) {
    return console.log(err)
  }

  require('./app/routes')(app, database);

  app.listen(PORT, () => {
    console.log("We are pouring beer at port " + PORT);
  });
});

我的应用程序的一般文件结构是:

Root/ 
  app/
    routes/
      index.js
      test_routes.js
      brewery_routes.js
      beer_routes.js
      city_routes.js
  server.js

奇怪的是,当我查看控制台日志时,我没有看到我的 /test 路由有任何错误,但其他路由返回时却出现了这个巨大的字体错误列表。正如您在我的代码中看到的那样,我没有加载任何字体,所以我想知道这是否是一个比我意识到的更大的问题。

enter image description here

有什么建议吗?

最佳答案

index.js 中,您一遍又一遍地将多项内容分配给 module.exports。只有最后一个能够存活下来并可供需要该模块的任何人使用。您没有显示加载 index.js 并尝试使用这些路由的代码,所以我不知道正确的解决方案是什么,但这确实解释了为什么只有最后一条路线 /app/routes/test_routes.js在 index.js 中可以工作。

因此,对 module.exports 的这些分配中的每一个都会覆盖前一个。只有最后一个幸存下来并实际导出:

const beerRoutes = require('./beer_routes');
module.exports = function(app, db) {
  beerRoutes(app, db);
};

const breweryRoutes = require('./brewery_routes');
module.exports = function(app, db) {
  breweryRoutes(app, db);
};

const cityRoutes = require('./city_routes');
module.exports = function(app, db) {
  cityRoutes(app, db);
};

const testRoutes = require('./test_routes');
module.exports = function(app, db) {
  testRoutes(app, db);
};

这就像:

let a;

a = function() { console.log("1"); }
a = function() { console.log("2"); }

a();    // outputs "2"

这可能是你想要的:

module.exports = function(app, db) {
    require('./beer_routes')(app, db);
    require('./brewery_routes')(app, db);
    require('./city_routes')(app, db);
    require('./test_routes')(app, db);
}

然后,当加载模块并调用构造函数时,您将初始化所有这些路由。

关于javascript - 无法获取路由 express /nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143522/

相关文章:

node.js - 如何在 NodeJS 中创建具有唯一 id 的路由

javascript - 自动将名称 ="blah"标签添加到 <tr> 中的第 n 个 <td>

c# - 等待 ClientScript.RegisterStartupScript 完成

Javascript 多个圆形图

node.js - Jade 模板引擎中的未定义属性

node.js - 在发送给客户端之前劫持响应

javascript - 通过AJAX结果可见数据集

node.js - 当将代码拆分到 Nodejs 中的自定义模块时,尝试将变量传递给另一个模块时,它会变得未定义

javascript - Sequelize.js - 关联,2 个表

node.js - 如何将事件从nodeJS/express发送到Angular