[英]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.