Friday, 26 May 2017

"Cannot GET /db/read/getall" accessing node.js express API using reverse proxy nginx on digitalocean droplet

If I go to mysite.com/db/read/getall, I get a blank page which says:

  • "Cannot GET /db/read/getall"

If I kill the app.js file in PM2, heading to the same url (mysite.com/db/read/getall) gives me the error:

  • 502 bad gateway - nginx ubuntu x.xx

This makes me think the node app is running fine but there's something wrong with the routes or setup. I was hoping this stack answer would fix it but I tried the suggestion and got the '502 bad gateway' error after adding the "app.use('/db', router);" statement to my app.

Any ideas?

NGINX default file setup:


server {
    listen 80;


    server_name xxx.xx.xx.xxx; 

        location /db {
        proxy_pass http://localhost:3005;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Node.js / Express app.js:


         var express = require('express');

    // For encoded URL - receiving a stringifyd JSON sent via POST URL
    var bodyParser = require('body-parser');

    var http = require('http');
    var io = require('socket.io')(http);
    var app = express();

    var server = http.createServer(app).listen(3005, 'localhost');

    io = io.listen(server);

    var cors = require('cors');

    var path = require("path");

    var MongoClient = require('mongodb').MongoClient
        , assert = require('assert');

    var url = 'mongodb://localhost:27017/db1';


    app.get('/read/getall/', function (req, res) {


        MongoClient.connect(url, function (err, db) {
            assert.equal(null, err);

            var collection = db.collection('clientLogs');

            collection.find({}).toArray(function (err, docs) {
                assert.equal(err, null);


                res.setHeader('Content-Type', 'application/json');
                res.send(JSON.stringify(docs) /*, null, '\t'*/);


                db.close();
            });

        })

    });



via Jake

No comments:

Post a Comment