Reputation: 105
I have a simply nodeJS app that parses a youtube URL that stores a user's watch history and lets them bookmark videos. I store the watch history and bookmarks in localStorage.
I need to provide two routes in nodeJS to list the current history, and save a URL into a history table.
I have no back-end experience, but have managed to create the app using nodeJS. Any suggestions are welcome!
Relevant code:
//parse youtube url and change iframe src
function loadVideo(videoURL){
//split youtube url and get ID
var videoID = videoURL.split("v=")[1];
//change iframe src
videoSource = `https://www.youtube.com/embed/${videoID}`;
videoPlayer.src = videoSource;
logHistory(videoSource)
}
//create div element
function logHistory(videoSource){
var newHistory = document.createElement("li");
newHistory.classList.add("history-item");
newHistory.innerText = videoSource;
historyContainer.appendChild(newHistory);
//store element in localStorage
var historyCount = historyContainer.children.length;
localStorage.setItem("History item "+historyCount, videoInput.value)
}
Upvotes: 0
Views: 8250
Reputation: 1012
Note. This answer makes use of socket.io which was not in the question asked but allows the node server to communicate with client side scripts.
I'm going to share an example from an app I've created where I used node.js [and socket.io] and also local storage. Much like the others said, this cannot be done directly from your node.js server.
This code is from my server.js (node.js server) file.
function setStartingPlayer(startingPlayer, privateUsers) {
//send starting player name to
io.emit('set-starting-player', startingPlayer, privateUsers);
}
It emits to the sockets two pieces of data: startingPlayer & privateUsers. In this example startingPlayer is a player's user name and privateUsers is an array of names of current players.
In the client script there is a function like this:
socket.on('set-starting-player', function(name, privateUsers) {
setStorage('turnIndicator', username);
var username = getStorage('username');
if (name === username) {
console.log('I am the starting player. My name is ' + username);
}
let playerInGame = checkPrivateUsers(privateUsers);
if (playerInGame === true) {
updateTurnIndicator(name);
}
});
This function accesses local storage in two ways, it stores data to local storage and it gets data from local storage. This is an example of how you can pass data from Node.js to a client script and access local storage.
Worth noting: I have two javascript functions, this is how I access local storage.
function setStorage(key,info) {
localStorage.setItem(key, JSON.stringify(info));
}
function getStorage(key) {
var item = localStorage.getItem(key);
return JSON.parse(item);
}
If you have questions let me know. Hope this helps.
Upvotes: 0
Reputation: 9066
LocalStorage is a frontend concept. It is accessible to JavaScript running in the browser. Node.js resides on backend and can not access the LocalStorage directly.
You'd need to access it through the scripts running in frontend whcih can communicate with backend using Ajax requests.
Upvotes: 3