node.jsexpressparse-server

NodeJS/Express: app.use("/parse") throws Router.use() requires a middleware function but got a Object error


I am following parse-server installation guidelines, and more precisely trying to install it with NodeJS and Express.

As indicated in the guidelines, here is my code to setup the server:

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var app = express();

var api = new ParseServer({
    databaseURI: 'postgres://postgres:myStrongPassword@localhost:5432/postgres',
    appId: 'app',
    masterKey: 'master',
    serverURL: 'http://www.example.com:1337/parse'
});

app.use('/parse', api);

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

httpServer.listen(1337, function() {
    console.log('parse-server running on http://www.example.com:1337/parse');
});

However, when trying to launch the server with node index.js, I get this error:

/root/node_modules/express/lib/router/index.js:469
      throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a Object
    at Function.use (/root/node_modules/express/lib/router/index.js:469:13)
    at Function.<anonymous> (/root/node_modules/express/lib/application.js:227:21)
    at Array.forEach (<anonymous>)
    at Function.use (/root/node_modules/express/lib/application.js:224:7)
    at Object.<anonymous> (/root/parse-test/index.js:12:5)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Node.js v18.14.2

Could you please help me to fix this issue

Thank you


Solution

  • You weren't far off, according the docs (https://github.com/parse-community/parse-server#parse-server--express), you need:

    app.use('/parse', api.app);
    

    Full code:

    var express = require('express');
    var ParseServer = require('parse-server').ParseServer;
    var app = express();
    
    var api = new ParseServer({
        databaseURI: 'postgres://postgres:myStrongPassword@localhost:5432/postgres',
        appId: 'app',
        masterKey: 'master',
        serverURL: 'http://www.example.com:1337/parse'
    });
    
    app.use('/parse', api.app);
    
    var httpServer = require('http').createServer(app);
    
    httpServer.listen(1337, function() {
        console.log('parse-server running on http://www.example.com:1337/parse');
    });
    

    You may find you also need this line:

    // Start server
    await api.start();