使用express和mongodb服务动态URL [英] Serving dynamic URLs with express and mongodb

查看:62
本文介绍了使用express和mongodb服务动态URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个具有类似Reddit功能的网站.我希望用户提交的内容具有其自己的页面.每个提交都分配有一个5个字符的ID,我希望该ID出现在该页面的URL中.

我在路由器文件中有了此功能,该文件呈现了一个名为titles的页面:

exports.titles = function(req, res){
i = 0
read(function(post){
    url = post[i].URL;
    res.render('titles', {title: post[i].title, url: post[i].URL});
});

};

它由app.js中的此语句提供服务:

app.get('/titles', home.titles); //home.js is the router file

标题页面上有一个链接,其中包含文本post.title和URL post.URL.当用户点击链接(例如domain.com/12345)时,应将他们带到内容为content.post.body的页面.

我如何a)将URL传递回我的app.js文件以包含在app.get中,b)在此路由器文件中包含app.get函数,或c)以其他任何方式解决此问题? /p>

我确实有一个对象"titles",它是一个mongodb集合,但是它在另一个模块中.我没有理由不能将其添加到路由器中.

我尝试将其添加到app.js中以查看其是否有效:

app.get('/:id', function(req, res){
  return titles.findOne({ id: req.params.id }, function (err, post) {
    if (err) throw(err); 

    return res.render('content', {title: post.title, content: post.body});
   });
});

我明白了.我所做的只是格式化标题,使其看起来像domain.com/titles/12345,然后将app.get('/:id'更改为app.get('/titles/:id,...

解决方案

如果我答对了,我会反过来做.

简短版

  1. 我将从网址中获取id
  2. 然后我将从数据库中提取与此id
  3. 相关的数据
  4. 并使用这些数据来构建最终页面.

您无需为每个URL创建新的路由. URL可以包含一些变量(此处为id),Express可以解析URL以获取此变量.然后从此id您可以获得构建正确页面所需的数据.

长版

我假设有人输入以下URL:http://domain.com/1234.
我还假设您有一个变量titles,它是MongoDB集合.

您可以这样定义一条路线:

app.get('/:id', function(req, res) {
  // Then you can use the value of the id with req.params.id
  // So you use it to get the data from your database:
  return titles.findOne({ id: req.params.id }, function (err, post) {
    if (err) { throw(err); }

    return res.render('titles', {title: post.title, url: post.URL /*, other data you need... */});
  });
});

编辑

我根据最近的评论做了一些更改...

I'm building a site that has somewhat reddit-like functionality. I want user-submitted content to get its own page. Each submission is assigned a 5 character ID that I want to be in the URL for that page.

I've got this function in the router file which renders a page called titles:

exports.titles = function(req, res){
i = 0
read(function(post){
    url = post[i].URL;
    res.render('titles', {title: post[i].title, url: post[i].URL});
});

};

It is served by this statement in app.js:

app.get('/titles', home.titles); //home.js is the router file

The titles page has a link with the text post.title and the URL post.URL. When a user clicks on the link (e.g. domain.com/12345) they should be taken to a page called content with the content post.body.

How do I a)pass the URL back to my app.js file to include in an app.get, b) include the app.get function in this router file, or c) solve this in any other way?

Edit: I do have an object 'titles' that is a mongodb collection, but it is in a different module. No reason I can't add it to the router though.

Edit: I tried adding this to app.js to see if it would work:

app.get('/:id', function(req, res){
  return titles.findOne({ id: req.params.id }, function (err, post) {
    if (err) throw(err); 

    return res.render('content', {title: post.title, content: post.body});
   });
});

Edit: I got it to work. All I did was format the title so that it would look like domain.com/titles/12345 and change app.get('/:id', to app.get('/titles/:id, ...

解决方案

If I get you right I would do that the other way around.

Short version

  1. I would get the id from the URL
  2. Then I would pull from the database the data associated with this id
  3. And use this data to build the final page.

You don't need to create a new route for each URL. An URL can contain some variable (here the id) and Express can parse the URL in order to get this variable. Then from this id you can get the data needed to build the proper page.

Long version

I assuming someone type in this URL: http://domain.com/1234.
I also assume that you have a variable titles which is a MongoDB Collection.

You can have a route defined like this:

app.get('/:id', function(req, res) {
  // Then you can use the value of the id with req.params.id
  // So you use it to get the data from your database:
  return titles.findOne({ id: req.params.id }, function (err, post) {
    if (err) { throw(err); }

    return res.render('titles', {title: post.title, url: post.URL /*, other data you need... */});
  });
});

Edit

I made some changes according to the last comments...

这篇关于使用express和mongodb服务动态URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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