如何使用JavaScript在服务器上编写文本文件? [英] How to write a text file on server using JavaScript?

查看:86
本文介绍了如何使用JavaScript在服务器上编写文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想仅使用JavaScript代码来更改服务器上存在的文件的内容.另外,如果需要,请随时考虑服务器是否授予对该文件的写访问权限.

I would like to change the contents of a file present on the server using only JavaScript code. Also, feel free to consider that the server grants write access to that file, if needed.

推荐答案

Node.js是最好的选择.它使您可以访问本地文件系统.

Node.js is the best for this. It gives you acces to your local file system.

示例:

var fs = require('fs');
fs.writeFile("/tmp/test.txt", "Hey there!", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
}); 

这篇关于如何使用JavaScript在服务器上编写文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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