简体   繁体   中英

Running NodeJS server on Heroku

I just installed a NodeJS server on Heroku by walking through the getting started guide ( https://devcenter.heroku.com/articles/getting-started-with-nodejs ) and I think I did all the steps correctly.

However, when I go to the url generated by heroku ( http://secure-caverns-6779.herokuapp.com/ ) it displays an Application Error. I don't know if this is expected behaviour for the .js server I wrote, but in any case it's not doing what I want.

This is what I want to do and tested correctly on localhost:

  1. From a webpage, send some get requests to the NodeJS server.

  2. From the NodeJS server make some calls to a MongoDB database.

  3. (From the NodeJS server, send some data back to another service, but thats not important here)

This is the server.js file I uploaded to Heroku and tested locally without problems (some parts omitted for brevity):

var http = require("http"),
    url = require('url'),
    req = require("request"),
    qs = require('querystring'),
    async = require("async"),
    mongojs = require("mongojs"),
    JSONStream = require('JSONStream'),
    open = require('open'),
    webURI = "[web uri here]";

var uri = "[mongoDB uri here]",
    db = mongojs.connect(uri, ["db_name"]);

var server = http.createServer(requestHandler);

...some functions (example below)...

function addTrack(post,callback){
    var partyId = post['partyId'], trackId = post['trackId'];
    db.db_name.update({id: parseInt(partyId)}, { $push: { tracks: trackId } }, function(err, added) {
        if( err || !added ) {
            console.log("Track could not be added.");
            callback(null,added);
        }
        else {
            console.log("Track("+trackId+") added to Party("+partyId+")");
            callback(null,added);
        }
    });
}

function requestHandler(request, response) {
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.writeHead(200, {"Content-Type": "application/json"});
    var pathname = url.parse(request.url, true).pathname;
    switch (true) {

        ...some switch cases...

        case request.method === 'POST' && /\/addTrack/.test(pathname):
            var body = '';
            request.on('data', function (data) {
                body += data;
                // Too much POST data, kill the connection!
                if (body.length > 1e6)
                    req.connection.destroy();
            });
            request.on('end', function () {
                var post = qs.parse(body);
                addTrack(post, function(err, added) {
                    response.write(JSON.stringify({ status: added }));
                    response.end();
                });
            });
            break;

    }
}

var port = Number(process.env.PORT || 5000);
server.listen(port, function() {
    console.log("Listening on " + port);
});

So, to reiterate, I tested this server.js locally and all worked fine. Now I'm testing it after uploading it to Heroku (did all the steps including creating the Procfile and package.json etc.) and it doesn't work. The url I'm using to send GET/POST requests is: http://secure-caverns-6779.herokuapp.com/ which I think is what the server.js should be at. Note I don't want the server.js to create a webpage or whatever, only handle the calls and communication with the MongoDB database.

And example jquery GET request I did from my webpage locally to the NodeJS server is the following:

$.get("http://127.0.0.1:8888/server.js/trackAdded", "partyId=" + partyId + "&trackId=" + value.id, function (json) {
     ...
}, 'json');

What's the error here, should I take a different url, add a port maybe? Add a "/server,js" to the url?

Kind regards,

在将项目部署到heroku的控制台中使用“ heroku日志”,并查找出现错误的地方:)

Based on what you said, Heroku should have replaced your base url http://127.0.0.1:8888/ by http://secure-caverns-6779.herokuapp.com/ .

So if http://127.0.0.1:8888/server.js/trackAdded works locally, http://secure-caverns-6779.herokuapp.com/server.js/trackAdded should work too. I've tested here and it return a json object:

{"status":0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM