Can feathers co exist with routs managed out side

2019-07-21 13:52发布

问题:

We have a large app which uses express for rest and primus for socket routes. It would be very hard to convert all to feathers at once. I am thinking of phased approach where I could take some routes and convert them to services and of cause any new routes will follow the service pattern. I will slowly migrate the rest of the app.

The client is using primus and angularjs $http for now to communicate with nodejs.

our current set up looks like

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);
        }
    });
};

I'd like to add feathers in this and then slowly start converting. Is this possible?

回答1:

Yes, with the standard @feathersjs/express framework integration your Feathers application will also be a fully Express compatible application which additionally allows to register services.

In your case you would replace var app = express(); with:

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());

And everything should continue to work as normal. The next step would be to replace the custom Primus code with the @feathersjs/primus framework adapter:

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' }));

Now you can also replace the http.createServer setup with a more simple

const server = app.listen(config.get(constants.CONFIG_App_Port))

Since Feathers will handle all the Express and Primus initialization. The Primus instance will be available as app.primus.



标签: feathersjs