我们有一个大型应用程序,它使用express来进行休息,使用primus进行套接字路由。立即将所有内容转换为羽毛非常困难。我正在考虑一种分阶段的方法,在该方法中,我可以采取一些路线并将其转换为服务,并且导致任何新路线都将遵循服务模式。我将慢慢迁移应用程序的其余部分。
客户端目前正在使用primus和angularjs $ http与nodejs进行通信。
我们当前的设置如下
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
const csrf = require('csurf');
var Primus = require('primus');
var SocketService = require('./../services/socket-service'); ////this handles existing socket routes from primus client using spark.write
var routesUtils = require('../utilities/routes-utility');
var _ = require('lodash');
module.exports = function(isClusteredDeploy) {
var app = express();
var server = http.createServer(app);
var primus = new Primus(server, {transformer: 'uws'});
var socketService = SocketService(primus);
var commonSocketRoute, commonRoute;
//primus.library();
//primus.save(__dirname + '/primus-client.js');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json({
strict: false,
limit: '1mb'
}));
app.use(cookieParser());
app.use(csrf({ cookie: true }));
app.use(function (err, req, res, next) {
if (err.code !== 'EBADCSRFTOKEN') {
return next(err);
}
res.status(403);
res.send();
});
app.use(function(req, res, next) {
res.cookie('XSRF-TOKEN', req.csrfToken());
next();
});
server.listen(config.get(constants.CONFIG_App_Port), function() {
log.info('App server ==> %s is listening on port %d', config.get('rest_host_config.' + config.get('app_key') + '.host'),
config.get(constants.CONFIG_App_Port));
});
//dynamically loading rest routes and socket routes from the file system
var files = routesUtils.readRoutes(true);
files.forEach(function(file) {
if (_.includes(file, 'socket')) {
commonSocketRoute = require('./../../' + file);
commonSocketRoute(socketService);
} else {
commonRoute = require('./../../' + file);
commonRoute(app);
}
});
};
我想在其中添加羽毛,然后慢慢开始转换。这可能吗?
答案 0 :(得分:2)
是的,通过标准的register services框架集成,您的Feathers应用程序也将是完全兼容Express的应用程序,并且还允许@feathersjs/primus。
在您的情况下,您可以将var app = express();
替换为:
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
// Create an app that is a Feathers AND Express application
const app = express(feathers());
// Set up REST services (optional)
app.configure(express.rest());
一切应该继续正常进行。下一步将使用blog框架适配器替换自定义Primus代码:
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const primus = require('@feathersjs/primus');
// Create an app that is a Feathers AND Express application
const app = express(feathers());
// Set up Primus with SockJS
app.configure(primus({ transformer: 'ws' }));
现在,您还可以将http.createServer
设置替换为更简单的
const server = app.listen(config.get(constants.CONFIG_App_Port))
因为Feathers将处理所有Express和Primus初始化。 Primus实例将以app.primus
的形式提供。