Kaustav Ray
Kaustav Ray

Reputation: 754

Deploy route in node.js application in heroku without express.js

I have 3 files :

  1. server.js containing node.js server (Using WebSocket-Node)
  2. client.js containing websocket code
  3. frontend.html containing the html content includes the client.js file.

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
    };
});

Local Folder Structure:

  1. .git
  2. node_modules // containing websocket
  3. client.js
  4. frontend.html
  5. server.js
  6. package.json

But somehow my application is not running and showing application error !

I want to first start the nodejs server and open frontend.html.

As I am starting with nodejs and heroku for first time cannot understand the exact problem !

  1. Is there a problem in routing or other things are causing this error ?
  2. Is express.js mandatory for routing ?

Upvotes: 0

Views: 950

Answers (1)

eddiezane
eddiezane

Reputation: 916

Heroku requires that your either provide a Procfile, which is a simple file that tells Heroku how to actually start your app, or specify scripts.start in your package.json.

// 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

Upvotes: 1

Related Questions