我有3个文件:
server.js
包含node.js服务器(使用WebSocket-Node)client.js
包含websocket代码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
};
});
本地文件夹结构:
- 的.git
- node_modules //包含websocket
- client.js
- frontend.html
- server.js
- 的package.json
醇>
但不知怎的,我的应用程序没有运行并显示应用程序错误!
我想首先启动nodejs服务器并打开frontend.html。
因为我第一次开始使用nodejs和heroku无法理解确切的问题!
答案 0 :(得分:1)
Heroku要求您提供Procfile,这是一个简单的文件,告诉Heroku如何实际启动您的应用,或在scripts.start
中指定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