Using Node.js's File System Functions From A Browser
Solution 1:
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.
Solution 2:
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><scriptsrc="/jx?ms=connect"type="text/javascript"></script></head><inputtype="button"id="btnRemove"value="Remove temp.txt file"disabled="disabled"/><scripttype="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).
Solution 3:
Thanks for all the answers and comments. They've been helpful in pointing me to the right direction. What I actually wanted was to make a simple API call from the client. Here's what I did using express:
var app = express();
var settingsapirouter = require('./../../settings/settingsapi');
app.use('/api/settings', settingsapirouter);
In settingsapi
, I define:
module.exports = function (...)
{
var router = express.Router();
router.route('/restoredb')
.post(function (request, response) {
...
});
}
In the client's JS:
functionrestoreSettings()
{
$.ajax({
url: '/api/settings/restoredb',
type: 'POST',
})
.done(function() {
console.log("success");
});
}
Post a Comment for "Using Node.js's File System Functions From A Browser"