2

when i am using list.save() method a object other than customList name which is favicon.ico is also saving as record in following cod, Why am i gatting favicon.ico as object.

app.get('/:listRoute',function (req,res) {

    const customList=(req.params.listRoute);
    List.findOne({name:customList }, function (err,result) {
        if (!err) {
            if (!result) {
                const list=new List({
                    name: customList,
                    items: defaultItems
                })
                list.save();
            } else {
                console.log(result);

                res.render('list', {
                    listTitle: result.name,
                    latestItems: result.items})
            }
            } 

    });
    })
walters
  • 27
  • 4

4 Answers4

6

When you visit a website (any URL on that website), a browser will typically also send a request to that same domain for /favicon.ico so see if the web site offers an icon to be a visual representation of the site.

Since you are using a wildcarded top level route:

app.get('/:listRoute', ...)

That will get hit by the request for /favicon.ico. Some other urls you also may need to watch out for being requested are: /robots.txt, /humans.txt, /sitemap.xml, /ads.txt.


There are a number of ways to work around this:

  1. Your wildcard route can first check req.url or req.params.listRoute to see if it's something it should ignore.

  2. You can place other top level routes that you want to keep out of your wildcard route in a position before this route so they don't end up in this one.

  3. Don't use a top level wildcard route. Instead, use something like /list/:listRoute so it won't automatically match any top level http request. Your use of a top level wildcarded route interferes with other future uses of your site and can create backwards compatibility going forward when you want to add other top level routes to your site. Imagine if sometime in the future, you want to add /contact or /login or /logout. Those all conflict with /:listRoute.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Try to add a callback function to the list.save(); Let me know if this works. The reason is maybe because of sync issues. eg: time taken by mongoDB to update the first document & save > the time taken by the 'Get' method to redirect to itself. Therefore by adding this callback it kinda make sure the code gets saved first and err checked before the redirect.

eg:

list.save(function(err){
    if(!err) {
    console.log("list is successfully saved"); //log is optional
    res.redirect("/" + listRoute);
   }
  });
0

When fetching route data using params with express,the entered data easily we can log.But if not adding top-level route and just trying to get the required route eg:

app.get("/:requireddata",function(req,res){

const data = req.params.requireddata;

});

in this case, when loading main page the favicon.ico will generate as a result. So for getting an exact result, that's when only loading requireddata route we can get the result by using higher level route. In case there is no higher-level route add just an additional route before requireddata as shown below:

app.get("/add/:requireddata",function(){});

Here /add/ is an additional route for avoiding favicon.ico

For me this worked, so if this information is useful just go head.

Akash J
  • 81
  • 1
  • 2
0

Hey there I also came across this exact issue. So here is my solution to that. Just enclose everything in a if block and there you go. DONE !!!!

app.get("/:name", function (req, res) {
if (req.params.name != "favicon.ico") {
const name = _.capitalize(req.params.name);

List.findOne({ name: name }, (err, foundList) => {
  if (!err) {
    //new list with default items created
    if (!foundList) {
      const list = new List({
        name: name,
        items: defaultItems,
      });
      list.save();
      res.redirect("/" + name);
    } else {
      res.render("list", {
        listTitle: foundList.name,
        newListItem: foundList.items,
      });
    }
  }
});
}
});

P.s.:- It will throw some error from mongo but that'll not affect the overall working.

Hope this helps.