通过浏览器使用node.js的文件系统功能 [英] Using node.js's File System functions from a browser

查看:65
本文介绍了通过浏览器使用node.js的文件系统功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个从服务器上删除文件的功能.我打算使用此功能将设置(即db文件)恢复为默认状态.我使用以下命令运行服务器:

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

我知道node.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?

推荐答案

感谢所有答案和评论.他们为我指出正确的方向提供了帮助.我真正想要的是从客户端进行一个简单的API调用.这是我使用express所做的:

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);

settingsapi中,我定义:

module.exports = function (...) 
{
    var router = express.Router();

    router.route('/restoredb')
        .post(function (request, response) {
              ...
              });
}

在客户的JS中:

function restoreSettings()
{
    $.ajax({
        url: '/api/settings/restoredb',
        type: 'POST',
    })
    .done(function() {
        console.log("success");
    }); 
}

这篇关于通过浏览器使用node.js的文件系统功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆