Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 703 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Sapper-compatible setup to serve multiple apps from a single Express/Polka entry point

#1
I need to set up a Polka (or Express) server so that it can serve multiple applications (one per hostname) from a single *server.js* entry point. It can be done using the `vhost` middleware (

[To see links please register here]

). Each app is exported as a middleware, and the one that corresponds to each request is added in the middleware chain in *server.js*.

However, some (not all) of these apps will be Sapper applications so the setup must be compatible with Sapper. So far as I am aware, Sapper builds generate a *server.js* file which works as entry point for the app, but the app is not exported. **Is there a build option to export the Sapper app (instead of doing `listen` on it)? Or some other way to do this?**

I've tried to manually edit a build and it seems to work, although there are some issues with file paths because the root of the Sapper app is not that of the main application.

I've looked up this problem but didn't find any reference to it, so I am wondering if I am taking a wrong path and if there is a more obvious solution. (Note: the Node.js hosting I am using doesn't allow to map hostnames to app folders, which would of course make things simpler.)
Reply

#2
Solution #1: Export Sapper app as a middleware
------------

You can make your own export of Sapper app. In server.ts/server.js of your Sapper app, instead of starting the server you can export a middleware. Something like this:


```typescript
import * as sapper from "@sapper/server";
export const handler = sapper.middleware();
```

Then in your express app just map the domain to exported middleware:

```javascript
const express = require('express');
const vhost = require('vhost');
const path = require('path');
const { handler } = require('./__sapper__/build/server/server');


const app = express();

app.use(vhost('*.example.com', (req, res, next) => {

if (req.vhost[0] === 'sapper') {
return handler(req, res, next);
}

return res.statusCode(400);
}))

app.listen(3000, () => console.log('Server started'));

```

Solution #2: Use a reverse proxy
------
Another solution would be to run all the nested applications in different ports on the server and use a revers proxy to route the requests accordingly.

For running the nested applications on different ports, possible solution would be dockerizing them or using the [pm2][1].

For the reverse proxy part you could use Nginx or [Traefik][2]. Also it is possible to write a reverse proxy middleware to handle it programmatically using [http-proxy-middleware][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through