【问题标题】:Deploy route in node.js application in heroku without express.js在没有 express.js 的 heroku 中部署 node.js 应用程序中的路由
【发布时间】:2015-07-19 10:15:51
【问题描述】:

我有 3 个文件:

  1. server.js 包含 node.js 服务器(使用 WebSocket-Node)
  2. client.js 包含 websocket 代码
  3. 包含 html 内容的frontend.html 包含client.js 文件。

package.json:

{
  "name": "kapp",
  "version": "1.0.0",
  "description": "Lightweight peer to peer",
  "main": "frontend.html",
  "scripts": {
    "test": "node server.js"
  },
  "engines": {
    "node": "0.10.x"
  },
  "author": "Kaustav Ray",
  "license": "MIT",
  "dependencies": {
    "websocket": "^1.0.19"
  }
}

server.js

var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {

});
server.listen(1337, function() { });


wsServer = new WebSocketServer({
    httpServer: server
});


wsServer.on('request', function(request) {
    var connection = request.accept(null, request.origin);


    connection.on('message', function(message) {
        if (message.type === 'utf8') {

        }
    });

    connection.on('close', function(connection) {

    });
});

client.js

$(function () {

    window.WebSocket = window.WebSocket || window.MozWebSocket;

    var connection = new WebSocket('ws://127.0.0.1:1337');

    connection.onopen = function () {

    };

    connection.onerror = function (error) {

    };

    connection.onmessage = function (message) {

        try {
            var json = JSON.parse(message.data);
        } catch (e) {
            console.log('This doesn\'t look like a valid JSON: ', message.data);
            return;
        }
        // handle incoming message
    };
});

本地文件夹结构:

  1. .git
  2. node_modules // 包含 websocket
  3. client.js
  4. 前端.html
  5. server.js
  6. package.json

但不知何故,我的应用程序没有运行并显示应用程序错误!

我想先启动 nodejs 服务器并打开 frontend.html。

当我第一次开始使用 nodejs 和 heroku 时,我无法理解确切的问题!

  1. 是路由有问题还是其他原因导致了这个错误?
  2. express.js 是否必须用于路由?

【问题讨论】:

  • 你能告诉我们server.js中的代码吗?
  • 我已经粘贴了服务器和客户端的基本骨架 + 更新了 package.json,它在本地机器上运行良好,但在 heroku 中遇到问题!

标签: node.js heroku


【解决方案1】:

Heroku 要求您提供 Procfile,这是一个告诉 Heroku 如何实际启动您的应用程序的简单文件,或者在您的 package.json 中指定 scripts.start

// Procfile
web: node server.js

// package.json
"scripts": {
  "start": "node server.js"
},

https://devcenter.heroku.com/articles/nodejs-support#default-web-process-type https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-12
    • 2013-07-09
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多