I want to create a function that removes a file from the server. I intend to use this function to restore settings (i.e. db files) to their default state. I run my server using the following command:
node server.js
I am aware of node.js's File System but I don't know how to use it on the browser side. I did some light reading on the issue and it seems to me that this kind of interaction is not possible due to security reasons. How do I go about this?
You cannot use Node's fs in the browser and have it directly affect the file system on the server. fs is for use on locally available file systems only. You will need to create an API or use some other technique.
As other answers are saying, calling node server's method directly from client side is not possible, however indirectly you can do it by sending a request from the browser. On Node.JS part you can create an http server, client would call it, then server may perform an action.
However you should keep in mind, that removing files this way may be dangerous and you need to make sure on the server side, that you allow to remove only what you really want to be removed. For example you may allow to delete files only from specific directories (not just any file!).
There are really multiple ways to do it. Either you can do it in pure node (http.Server class), or you can use some extra modules e.g. express (which helps you to create an http server with just few lines of code) or jxm (which helps you to create client-server messaging system - also with few lines of code).
Since I'm familiar with jxm, I've made a simple example for you:
server.js
var server = require('jxm');
var fs = require("fs");
server.setApplication("Hello World", "/", "STANDARD-KEY");
server.linkResource("/", ["./index.html", "text/html"]);
server.addJSMethod("removeFile", function (env, param) {;
if (fs.existsSync(param)) {
fs.unlinkSync(param);
server.sendCallBack(env, "File Deleted: " + param);
} else {
server.sendCallBack(env, "File does not exist: " + param);
}
});
server.start();
index.html
<!DOCTYPE html>
<html>
<head>
<script src="/jx?ms=connect" type="text/javascript"></script>
</head>
<input type="button" id="btnRemove" value="Remove temp.txt file" disabled="disabled"/>
<script type="text/javascript">
document.onjxready = function () {
jxcore.Start(function (status) {
var btn = document.getElementById('btnRemove');
btn.disabled = "";
btn.onclick = function () {
jxcore.Call("removeFile", "temp.txt", function (ret) {
alert(ret);
});
};
});
};
</script>
</body>
</html>
Below is how you can run it. Just once you need to install jxm module:
$ npm install jxm
Now you can start the server:
$ node server.js
After that you can go to the browser (http://localhost:8000/) and click the button.
Even though jxm works best with JXcore (they both come from the same team), it works with Node.JS as well (as the above example shows).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With