首页 > 解决方案 > Uppy 文档未定义 node.js

问题描述

我正在尝试在我的网站中实现文件上传程序,但是当我尝试包含它时,我收到一条错误消息,指出该文档未定义。这已经是我使用 browserify 将它包含在我的客户端了。但是,我不确定我做错了什么。任何帮助将不胜感激。我的代码如下。仅供参考,我使用 Jet Brains 的 IntelliJ Idea 作为我的 IDE,我让 IDE 生成我的节点 js 项目,因此它自动为我创建了一些包含路由的页面。

应用程序.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

require('./uppy');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

uppy.js(这是我放置文档中的 uppy 代码的地方)

// Import the plugins
const Uppy = require('@uppy/core');
const XHRUpload = require('@uppy/xhr-upload');
const Dashboard = require('@uppy/dashboard');

const uppy = Uppy()
    .use(Dashboard, {
        target: '#drag-drop-area',
        inline: true
    })
    .use(XHRUpload, { endpoint: 'https://api2.transloadit.com' });

uppy.on('complete', (result) => {
    console.log('Upload complete! We’ve uploaded these files:', result.successful)
});

索引.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href="../node_modules/@uppy/core/dist/style.css" />
    <link rel='stylesheet' href="../node_modules/@uppy/dashboard/dist/style.css" />

  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>

  <div id="drag-drop-area"></div>

    <script src="../bundle.js"></script>
  </body>
</html>

如您所见,我使用 bundle.js 通过 browserify 合并了 uppy.js 中的代码。我使用了 cli browserify uppy.js -o bundle.js。老实说,我不知道我做错了什么,所以如果你能帮助我,我真的很感激。谢谢!

更新:在我的控制台上,我也收到了错误:

http://localhost:3000/node_modules/@uppy/core/dist/style.css (net::ERR_ABORTED 404 (Not Found))
http://localhost:3000/node_modules/@uppy/dashboard/dist/style.css (net::ERR_ABORTED 404 (Not Found))
http://localhost:3000/bundle.js (net::ERR_ABORTED 404 (Not Found))

标签: node.jsejsuppy

解决方案


我 100% 只是在这里猜测,因为我看不到错误的完整代码/repo 和完整堆栈跟踪,但听起来你document在 Node.js 代码中引用,这就是引发此错误的原因。document是存在于浏览器 DOM 中的东西,只能被 UI 代码引用。了解作为后端服务器的 Node/Express 代码与在浏览器中呈现的演示文稿/UI 代码之间的区别很重要。


推荐阅读