Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial/Blog: how to mount LB app as Express/Koa middleware #1982

Closed
1 task
dhmlau opened this issue Nov 6, 2018 · 5 comments
Closed
1 task

Tutorial/Blog: how to mount LB app as Express/Koa middleware #1982

dhmlau opened this issue Nov 6, 2018 · 5 comments

Comments

@dhmlau
Copy link
Member

dhmlau commented Nov 6, 2018

Description / Steps to reproduce / Feature proposal

Per our discussion with @raymondfeng @bajtos and others, users can mix and match using LoopBack and Express, taking the best of both framework.
LB can have a facade application that can continue to use express for hosting capability but can compose different segment of the route.  LB app can be mounted to express / koa as middleware.

See also #1293 Allow app developers to configure custom Express middleware

Acceptance Criteria

  • Create a sample app built from the tutorial (see TodoList tutorial for reference)
@dhmlau dhmlau changed the title Tutorial/Blog to show how to mount LP app to Express/Koa as middleware Tutorial/Blog to show how to mount LB app to Express/Koa as middleware Nov 6, 2018
@bajtos
Copy link
Member

bajtos commented Nov 6, 2018

See #691 and #1818 for related discussions we have had in the past.

With the current code base and APIs, it's already possible to mount LB4 REST API as an Express middleware.

const mainApp = express();
const apiApp = new MyLb4Application();

mainApp.use('/api', apiApp.requestHandler);
mainApp.use(express.static('./assets'));

mainApp.listen(3000);

@dhmlau dhmlau added the TOB label Nov 15, 2018
@dhmlau dhmlau mentioned this issue Nov 20, 2018
20 tasks
@bajtos bajtos changed the title Tutorial/Blog to show how to mount LB app to Express/Koa as middleware Tutorial/Blog: how to mount LB app as Express/Koa middleware Nov 22, 2018
@drouillard
Copy link

drouillard commented Jan 7, 2019

This would be helpful. I know target market is API developers, but when getting started it is nice to be able to plug in views without having to launch a separate React App or similar.

I ended up doing this which I can't imagine is your desired solution :)

// @ts-ignore
  const expressApp = app.restServer._expressApp;

  expressApp.set('view engine', 'ejs');
  expressApp.set('views', path.join(__dirname, 'views'));

I looked into overriding Rest Server and Rest Application but the learning curve was too much for a days worth of trial / error. The above solution of mounting inside of an Express works well and is cleaner. I wanted to try out loopback-authentication though hence the hack I did.

(Overall though, lb4 seems promising. Congrats on GA release)

@jannyHou
Copy link
Contributor

jannyHou commented Jan 7, 2019

@drouillard Thank you for sharing a solution from a different perspective! I think your approach and the one in the original comment serves different requirements:

launching an express app

Developers can take this approach when they only want to mount loopback app's APIs as a whole without interfering with its implementation details.

build routes on top of express app in rest server

As you explained, it's good for adding plugin like views on top of an existing loopback app.

We could investigate and document both.

@drouillard
Copy link

@jannyHou good clarification. I was able to interfere with some of the LB4 implementation details even when mounting it as above by setting a view engine on the express app at large. However, setting the base views directory did not work, hence the hack above. It makes sense though, as that is not intended use, nor should it be. I was thinking originally that I would be able to create an inherited version of the Rest Server or someone how along configs to it. However, I am guessing that is a poor solution in general given the evolution of LB3 to LB4. Seems like the current implementation is locked down due to issues with people hacking at express and then it causing 'loopback' issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants