I am Making An eCommerce website using
NodeJS + NginX MongoDB Html(ejs),CSS,JS - I know its bad decision for not using framework - Client Demand :( I want there should be a cart system and add to favourite system I know developers are using localStorage mostly for cart . What should I use from these three options for cart and favourite system and why
I would suggest Cookies! If you add the items to cookies, you can set them to auto-expire. For example, if the user has added something to the cart, you can automatically delete it after 2 months or so. Another advantage of Cookies is that you can set it from Server too. So in order confirmation page or something, from the server itself, you can delete a user's cookies
But if you don't want to expire it, then go for LocalStorage. LocalStorage is more easy to set, get, delete etc. Since you're not using any front-end libraries you'll need to create functions for handling cookies (only a few lines though)
Database and cookies.
You should be saving any carts of logged in users to the database (for reference / recommendations later). And cookies are used as a backup for the current cart, or for guest carts.
You can use localstorage as long as security isn't an issue (which shouldn't be, since all requests should be server-validated anyway). I'd just lean on cookies cause they're more reliable (server can access them -- while browser can't).
I have always stored a UID in the cookie so I can identify the user during their session, and used that UID in my database for tracking items in the cart within the database.
Once the user logs in, I update the users record in the database to add their sessions UID while still tracking their session with cookies.
I am yet to use localStorage myself so can't comment on whether that would be better then using cookies or not.
well the question is how do you want the cart to behave ? should it be local? or account bound? as soon as you go for an account in combination with a cart there is only 1 solution that works cross browser.
So I personally would go for the Database you can however store a cart without an account via a specific ID and this id can be stored in the localStorage so the use can reopen it on the browser
every cart that has not been opened for a certain amount of time can be deleted. But maybe someone can argue for a different approach like the Session since you see the user as 'shopping right now' and you don't care about the past or a later.
it depends how you want the user to experience your shop
I would do cookie AND database -- otherwise how do you know who's who. A 'frequent purge' (four times a day from cron, deleting those > 24 hours old) of guest carts whilst RETAINING logged in user carts (which should be a thing) being pretty much the norm.
If you're selling users might want an account so they can access their cart from a different machine later. JUST cookies can't do that, localstorage sure as shine-ola can't do that.
I don't know what developers you're dealing with who put carts into localstorage, but that's the most jacktarded rubbish I've ever heard. PARTICULARLY since nothing client-side should EVER be trusted in that way. EVEN if you use AJAX to enhance the experience you should FIRST be writing your shopping cart so that it works WITHOUT CLIENT SIDE SCRIPTING! That way it is accessible to EVERYBODY.
Right now blocking scripting is as popular as ever -- in locked down workplaces, amongst the security minded, amongst those choked out under bandwidth caps -- hence plugins like scriptsafe, ghostery, noscript, etc, etc, all being popular.
This is even more true when your objective is to SELL THINGS. Build it to work with sessions, database, and normal HTML form submits first! THEN go through with scripting to enhance it with AJAX to avoid the page-loads for a smoother user experience for those with JS available/enabled.
Again, progressive enhancement, that way the page can gracefully degrade to be useful for everyone. If you do anything that means people can't shop without JavaScript, you're not doing your job, you're not doing the client any favors, and you're telling large swaths of the public to sod off!
... with both cookies-only and localstorage-only being client side scripting only solutions; aka utter and complete TRASH. Anyone telling you otherwise is either unqualified to their yap on the huffing topic, or is selling something.