【发布时间】:2018-07-24 23:12:31
【问题描述】:
在项目nodeJS - express中,通过npm安装模块后,当app.js const cf = require('create-file')中需要“create-file”模块时,我不能在main.js中使用它,在控制台我有这个返回@ 987654322@ 当我运行节点时。
app.js
` const express = require('express');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const cf = require('create-file');
// init app
const app = express();
//Template engine setup
app.set('view engine','html');
app.engine('html',ejs.renderFile);
// Index route
app.get('/',(req, res) => {
res.render('index.html');
})
// Public folder setup
app.use(express.static(__dirname + '/public'));
// Body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Catch from submit
app.post('/', (req, res) => {
res.send(req.body);
})
// Define port
const port = 3030;
// Start server
const server = app.listen(port, () => console.log( '服务器在端口启动 3030 '));`
main.js
`const button12 = document.getElementById('button');
button12.onclick = function () {
cf('C:\Users\tmeda\Bureau\fileTest.txt', 'my content\n', function (err) {
// file either already exists or is now created (including non existing
directories)
});
}`
【问题讨论】:
-
为什么不在 main.js 中再要求一次?
-
我不能,
require is not defined -
尝试编辑您的问题并添加您的代码,我认为您只是缺少一些东西
-
@YouneL 有想法吗?
-
您不能在客户端使用 create-file 包,它只能从服务器获得,而且浏览器不会让您出于安全目的创建文件,问题是您为什么要创建一个客户端机器上的文件?