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

Mounting parse dashboard on to a router won't work. #698

Closed
3 tasks done
tomasgvivo opened this issue Apr 22, 2017 · 7 comments
Closed
3 tasks done

Mounting parse dashboard on to a router won't work. #698

tomasgvivo opened this issue Apr 22, 2017 · 7 comments

Comments

@tomasgvivo
Copy link

tomasgvivo commented Apr 22, 2017

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Dashboard!

  • You're running version >=1.0.23 of Parse Dashboard.

  • You're running version >=2.3.2 of Parse Server.

  • You've searched through existing issues. Chances are that your issue has been reported or resolved before.

Steps to reproduce

var express = require('express');
var ParseDashboard = require('parse-dashboard');

var dashboard = new ParseDashboard({
  "apps": [
    {
      "serverURL": "http://localhost:1337/parse",
      "appId": "myAppId",
      "masterKey": "myMasterKey",
      "appName": "MyApp"
    }
  ]
});

var app = express();

// Here is where the problem starts...
var router = express.Router();

// make the Parse Dashboard available at /dashboard
router.use(dashboard);

// mount the router.
app.use('/dashboard', router);

var httpServer = require('http').createServer(app);
httpServer.listen(4040);

The problem:

ParseServer doesn't care where you mount it to.
It allways use app.

module.exports = function(config, allowInsecureHTTP) {
  var app = express(); //   <----------------------------------------- The problem
  // Serve public files.
  app.use(express.static(path.join(__dirname,'public')));

  // Allow setting via middleware
  if (config.trustProxy && app.disabled('trust proxy')) {
    app.enable('trust proxy');
  }

  // wait for app to mount in order to get mountpath
  app.on('mount', function() { // <--------------------------------- More prblems
    const mountPath = getMount(app.mountpath);
    const users = config.users;
    const useEncryptedPasswords = config.useEncryptedPasswords ? true : false;
    const authInstance = new Authentication(users, useEncryptedPasswords, mountPath);
    authInstance.initialize(app);

    // CSRF error handler
    app.use(function (err, req, res, next) {
      if (err.code !== 'EBADCSRFTOKEN') return next(err)

      // handle CSRF token errors here
      res.status(403)
      res.send('form tampered with')
    });

[...]

I have some idea of how to solve this.. but maybe there is a better one.

Btw, sorry for my bad english.

@flovilmart
Copy link
Contributor

flovilmart commented Apr 22, 2017

And if you use:

app.use('/dashboard', parseDashboard)

?

@tomasgvivo
Copy link
Author

@flovilmart

That's not the case.
Is not allways posible to mount parseDashboard onto the app, especially when you have multiple frontend applications in the same project. That's why routers exists... so all the mounted middlewares don't need to share the same application singleton.

@flovilmart
Copy link
Contributor

flovilmart commented Apr 23, 2017

Not sure when calling app = express() returns a singleton.

Also, can you expand on what 'won't work', because I personally would have done:

var router = express.Router()
router.use('/dashboard', dashboard)
app.use(router)

Also see: https://gist.github.com/flovilmart/6e72ae9a5c61246d21a7465013e64d75

@tomasgvivo
Copy link
Author

tomasgvivo commented Apr 23, 2017

@flovilmart

Thanks, now I understand better the problem.
When you mount a sub-app onto a router, the sub-app's app.on('mount', [...]) is never called.
I don't know if it's a express bug or parse-dashboard bug.

Btw, I thought app = express() returned a singleton.. every day you learn something new.

@flovilmart
Copy link
Contributor

I would say an express one, or you can probably forward the router on mount call to the dashboard by emitting the mount event back.

@tomasgvivo
Copy link
Author

It's seams it's a feature..
expressjs/express#3285

But now I know how to solve my problem.

Thanks.

@flovilmart
Copy link
Contributor

I don't believe there is any need to use a Router unless 100% necessary.

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

No branches or pull requests

2 participants