gpt4 book ai didi

node.js - 使用express将tus-node-server集成到feathersjs中

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:13 25 4
gpt4 key购买 nike

我有一个现有且正常工作的 Feathers 项目,采用最新的 Feathers cli(“Buzzard”)结构。现在我正在尝试添加 tus-node-server作为上传服务器。正如您可以在链接中找到的,将其设置为快速中间件的说明如下:

const tus = require('tus-node-server');
const server = new tus.Server();
server.datastore = new tus.FileStore({
path: '/files'
});

var app = express();
const uploadApp = express();
uploadApp.all('*', server.handle.bind(server));
app.use('/uploads', uploadApp);
app.listen(port, host);

按照这些说明,我尝试将其添加到我的 app.js 文件中:

// tus node server
const tus = require('tus-node-server');
const tusServer = new tus.Server();
tusServer.datastore = new tus.FileStore({
path: '../uploads'
});

const uploadApp = express();
uploadApp.all('*', tusServer.handle.bind(tusServer));
app.use('/uploads', uploadApp);

为了完整起见,我给出了整个 app.js 文件:

const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const helmet = require('helmet');
const logger = require('winston');

const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');

const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./channels');

const sequelize = require('./sequelize');

const authentication = require('./authentication');

const app = express(feathers());

// Load app configuration
app.configure(configuration());
// Enable CORS, security, compression, favicon and body parsing
app.use(cors());
app.use(helmet());
app.use(compress());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));

// Host the public folder
app.use('/', express.static(app.get('public')));

// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio({
wsEngine: 'uws',
timeout: 120000
}));
app.configure(sequelize);

// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);
// Set up event channels (see channels.js)
app.configure(channels);

// tus node server
const tus = require('tus-node-server');
const tusServer = new tus.Server();
tusServer.datastore = new tus.FileStore({
path: '../uploads'
});

const uploadApp = express();
uploadApp.all('*', tusServer.handle.bind(tusServer));
app.use('/uploads', uploadApp);

// Configure a middleware for 404s and the error handler
app.use(express.notFound());
app.use(express.errorHandler({ logger }));

app.hooks(appHooks);

module.exports = app;

我收到此错误:

# npm start

> api_server@0.0.0 start /home/usr/api_server
> node src/

/home/usr/api_server/node_modules/@feathersjs/express/lib/index.js:11
throw new Error('@feathersjs/express requires a valid Feathers application instance');
^

Error: @feathersjs/express requires a valid Feathers application instance
at feathersExpress (/home/usr/api_server/node_modules/@feathersjs/express/lib/index.js:11:11)
at Object.<anonymous> (/home/usr/api_server/src/app.js:70:19)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/usr/api_server/src/index.js:3:13)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:617:3

应该如何设置?如何将其最好地融入到 Feathers 中?来自 this我的印象是服务和 Hook 设置比中间件设置更好!?

最佳答案

供引用,来自GitHub issue的解决方案是使用 require('@feathersjs/express').originalrequire('express') 来初始化应用程序:

const createExpressApp = require('express');
const uploadApp = createExpressApp();

uploadApp.all('*', tusServer.handle.bind(tusServer));
app.use('/uploads', uploadApp);

或者通过在您的应用中访问 require('@feathersjs/express').original (express 模块 @feathersjs/express 依赖),如下所示:

const uploadApp = express.original();

uploadApp.all('*', tusServer.handle.bind(tusServer));
app.use('/uploads', uploadApp);

关于node.js - 使用express将tus-node-server集成到feathersjs中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48412192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com