简体   繁体   English

Node.js-如何在Express和Mongoose中按名称路由?

[英]Nodejs - How to route by name in url with express and mongoose?

I have an Express 4 server with CRUD routes for authors: 我有一个具有作者CRUD路由的Express 4服务器:

    router.get('/authors', AuthorsController.index);
    router.post('/authors', AuthorsController.store);
    router.get('/authors/:name', AuthorsController.show);
    router.put('/authors/:name', AuthorsController.update);
    router.delete('/authors/:name', AuthorsController.remove);

Most tutorials I find will have the routes be /authors/:id and then do Author.findById(req.params.id) . 我发现的大多数教程的路由都是/authors/:id ,然后执行Author.findById(req.params.id) I'd instead like it to be the authors name so there are human readable URLs, like: /authors/jk-rowling . 取而代之的是,我希望它是作者的名字,因此有一些可读的URL,例如: /authors/jk-rowling

Do I need to store the hyphenated string on the Authors model, what's the best way to achieve this with express and Mongoosejs? 我是否需要将连字符的字符串存储在Authors模型上,用express和Mongoosejs实现此目的的最佳方法是什么?

My authors controller currently looks like this: 我的作者控制器当前看起来像这样:

const AuthorsController = {
  async index(req, res){
    const authors = await Author.find().populate('books');
    res.send(authors);
  }
};

(I'm using express-async-errors package for the async-await functionality with express) (我正在将express-async-errors包用于带有express的async-await功能)

What's the best way to establish routes with human readable URLs for a CRUD REST API with Express and Mongoose? 为带有Express和Mongoose的CRUD REST API建立具有人类可读URL的路由的最佳方法是什么?

You can index name field and in your views, find by name (assuming name is attribute of Author) 您可以索引名称字段,并在视图中按名称查找(假设名称是“作者”的属性)

This requires no change in terms of db model. 这不需要更改数据库模型。 You can search via both id or name if needed. 您可以根据需要通过ID或名称进行搜索。

For example 例如

 router.get('/authors/:name', AuthorsController.show);

will have below view 将有下面的看法

const AuthorsController = {
  async show(req, res){
    const authors = await Author.find({'name':req.params.name}).populate('books');
    res.send(authors);
  }
};

As you mentioned in problem, you will have to generate slug for name like one containing hyphens in place of spaces or other special characters which will be stored in model as well. 正如你在问题中提到,你将不得不产生像含代替空格或其他特殊字符连字符一个将被存储在模型和名称蛞蝓

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 nodejs 中的快速获取路由从 url 中提取页面名称和 authToken? - How to extract page name and authToken from url using express get route in nodejs? 使用参数获取NodeJS中的Express Route URL - Get Express Route URL in NodeJS with parameters 如何使用 NodeJS、Express 和 Mongoose 使 get 方法在单击按钮时使用参数读取 url - How to make the get method read the url with params on button click using NodeJS, Express, and Mongoose 如何使用mongoose,nodeJs(Express)在数据库中插入具有相同字段名称的多个字段数据 - How to insert multiple field data with same field name in database with mongoose, nodeJs ( Express ) 如何在Express中路由动态URL - How to route dynamic URL in Express 如何使用Express和Node.js保护静态路由 - How to secure a static route with Express and Nodejs 如何在Express Node.js上提取路由名称(路径)(在通话期间,从请求中) - How can I extract the route name (path) on express nodejs (during the call, from the req) 在Express / nodejs中参数化路由URL之后添加静态文件 - static files are getting added after parameterized route url in Express/nodejs 如何使用2种模式,如何正确保存Mongoose,nodejs,express - how to work with 2 schemas, how to save correctly Mongoose, nodejs, express Mongodb / Mongoose:如何在快速路由上正确实现findOneAndUpdate - Mongodb/Mongoose: How to properly implement findOneAndUpdate on an express route
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM