繁体   English   中英

如何用nodejs写多个文件并表达

[英]How to write multiple files with nodejs and express

我正在使用 3 个文本区域构建自己的代码编辑器:html、css 和 javascript。 此代码编辑器会将文本区域的数据分别保存到单个文件中。

使用 express 和 nodejs,我设法只保存来自 html 文本区域的数据。

function htmlFile(file, data, response) {
var name = "files/" + file + ".html";
var content = data;
fs.writeFile(name, content, function (err) {
if (err) {
    throw err;
    response.send(err);
} else {
    response.send(' index.html file saved at: ' + name + ') Notes: ');
}
});
};

// static directory
app.use(express.static('static'))

// Home Page: index.html
app.get('/', (req, res) =>  {
res.sendFile('./static/index.html');
});

// create page
app.get('/page', (req, res) =>  {
res.sendFile(__dirname + '/static/index.html');
});

app.post('/page', function (req, response) {
var file = req.body.file;
var content = req.body.myPage;
htmlFile(file, content, response);
});

所以,我的问题是:

是否可以从不同的文本区域保存多个文件?

主要目标是将 css textarea 的内容保存为 style.css 并将 javascript textarea 的内容保存为 scripts.js

谢谢你。

在您的网页中,将所有文本区域元素放在<form>中,然后确保每个文本区域都有name属性。 然后在提交时,您的POST请求将包含您可以在 Express 中使用req.body.nameAttributeOfRespectiveTextArea访问的每个文本区域的内容。 然后根据您的需要保存内容!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM