node.js - 我可以通过将代码 NODE.js 放在要从 HTML 调用它的 JS 文件中来创建文件并向其写入值吗?
问题描述
这是从 VSC 终端运行时有效的简单代码。
const fs = require('fs');
const path = require('path');
fs.mkdir(path.join(__dirname, 'json'), err => {
if (err) {
throw err;
}
console.log('File created');
});
const filePath = path.join(__dirname, 'json', 'temp.txt');
fs.writeFile(filePath, 'Some value', err => {
if (err) {
throw err;
}
console.log('Value was signed into the file');
});
现在,我将同样的东西放入 JS 文件中,其中有一些计算,我想将结果写入文件。这不起作用。
project_data = [];
project_data[0] = parseInt(initial_data);
for (i=1; i < len_table; i++) {
project_data[i] = parseInt(project_data[i-1]) + 1;
}
const oil_production_map = new Map();
for (i = 0; i < len_table; i++) {
oil_production_map.set(project_data[i],parseInt(oil_production[i]));
}
const fs = require('fs');
const path = require('path');
fs.mkdir(path.join(__dirname, 'json'), err => {
if (err) {
throw err;
}
alert('File created');
});
const filePath = path.join(__dirname, 'json', 'oil_production_file.txt');
fs.writeFile(filePath, 'Some value', err => {
if (err) {
throw err;
}
alert('Value was signed into the file');
});
解决方案
推荐阅读
- python - 经过训练的 GradientBoostedClassifier 将零重要性分配给任何特征
- laravel - Laravel 5.5 图像未从存储/应用程序中显示
- javascript - 如何通过 v-model 设置选中复选框的计算属性?
- java - java泛型如何确保类型具有指定的注释?
- javascript - React JS .map 返回所有数据的时间与 JSON 中的对象相同
- docker - 有没有办法在 Kubernetes 上缓存加载的图像
- spring-mvc - 为什么用 @ModelAttribute 注释的方法无法获取通过视图添加的模型属性
- javascript - 文本框点不接受符号和大写字母而无需在 html/php 中单击按钮?
- select - 如何在 vuejs2 中的 api 调用后更新 v-chosen
- c++ - 在 C++ 中,如何从 ClassB 访问 ClassA 中的多维数组?