简体   繁体   English

在Heroku上运行NodeJS服务器

[英]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. 我只是通过逐步阅读入门指南( https://devcenter.heroku.com/articles/getting-started-with-nodejs )在Heroku上安装了NodeJS服务器,我认为我正确地完成了所有步骤。

However, when I go to the url generated by heroku ( http://secure-caverns-6779.herokuapp.com/ ) it displays an Application Error. 但是,当我转到heroku生成的URL( http://secure-caverns-6779.herokuapp.com/ )时,它会显示应用程序错误。 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. 我不知道这是否是我编写的.js服务器的预期行为,但是无论如何它都没有满足我的要求。

This is what I want to do and tested correctly on localhost: 这是我想做的,并在localhost上正确测试:

  1. From a webpage, send some get requests to the NodeJS server. 从网页上,将一些获取请求发送到NodeJS服务器。

  2. From the NodeJS server make some calls to a MongoDB database. 从NodeJS服务器对MongoDB数据库进行一些调用。

  3. (From the NodeJS server, send some data back to another service, but thats not important here) (从NodeJS服务器,将一些数据发送回另一个服务,但这在这里并不重要)

This is the server.js file I uploaded to Heroku and tested locally without problems (some parts omitted for brevity): 这是我上载到Heroku并在本地测试没有问题的server.js文件(为简洁起见,省略了某些部分):

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. 因此,重申一下,我在本地测试了该server.js,并且一切正常。 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. 现在,我将其上传到Heroku后进行了测试(完成了包括创建Procfile和package.json等在内的所有步骤),并且它不起作用。 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. 我用来发送GET / POST请求的URL是: http : //secure-caverns-6779.herokuapp.com/ ,我认为这是server.js应该位于的位置。 Note I don't want the server.js to create a webpage or whatever, only handle the calls and communication with the MongoDB database. 注意,我不希望server.js创建网页或其他任何内容,仅处理与MongoDB数据库的调用和通信。

And example jquery GET request I did from my webpage locally to the NodeJS server is the following: 我从本地网页向NodeJS服务器发出的示例jQuery GET请求如下:

$.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? 在网址中添加“ / server,js”?

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/ . 根据您所说的内容,Heroku应该用http://secure-caverns-6779.herokuapp.com/替换了基本URL http://127.0.0.1:8888/

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. 因此,如果http://127.0.0.1:8888/server.js/trackAdded在本地工作,则http://secure-caverns-6779.herokuapp.com/server.js/trackAdded应该工作。 I've tested here and it return a json object: 我在这里进行了测试,它返回一个json对象:

{"status":0}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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