-FIXED-无法获取/表达和节点js

时间:2016-02-26 10:00:01

标签: node.js express socket.io openshift

嘿伙计们和一些朋友在一个项目上工作,我们希望我们的服务器在openshift上运行没有错误,但总是给不了/ 我试图寻找解决方案并试了很多,但它只是做了修复它。

任何想法?

var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);


//


app.use(express.static(__dirname + '/public'));

app.use('/static', express.static(__dirname + '/public'));



server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);

io.on('connection', onSocketConnection);

var players = [];

var npc1 = new Player(1049, 980);
npc1.id = "Johan Kruijs";
npc1.color = "gold";
npc1.name = "[NPC] Johan Kruijs";

players.push(npc1);

function onSocketConnection(client) {
    console.log("Player has connected: ");
    client.on("disconnect", onClientDisconnect);
    client.on("new player", onNewPlayer);
    client.on("move player", onMovePlayer);
};

function onClientDisconnect() {
    var removePlayer = playerById(this.id);

    if (!removePlayer) {
        console.log("Player not found: " + this.id);
        return;
    }

    console.log(removePlayer.name + " has disconnected.");

    players.splice(players.indexOf(removePlayer), 1);
    this.broadcast.emit("remove player", {
        id: this.id
    });
};

function onNewPlayer(data) {
    var newPlayer = new Player(data.x, data.y);
    newPlayer.id = this.id;
    newPlayer.name = data.name;
    newPlayer.color = data.color;

    this.broadcast.emit("new player", {
        id: newPlayer.id,
        x: newPlayer.x,
        y: newPlayer.y,
        name: newPlayer.name,
        color: newPlayer.color
    });

    var i, existingPlayer;
    for (i = 0; i < players.length; i++) {
        existingPlayer = players[i];
        this.emit("new player", {
            id: existingPlayer.id,
            x: existingPlayer.x,
            y: existingPlayer.y,
            name: existingPlayer.name,
            color: existingPlayer.color
        });
    };

    players.push(newPlayer);
    console.log(" - name: [" + newPlayer.name + "]")
    console.log(" - id:   [" + newPlayer.id + "]");
};

function onMovePlayer(data) {
    var player = playerById(data.id);
    player.x = data.x;
    player.y = data.y;
    player.id = data.id;

    io.emit("move player", {
        id: data.id,
        x: data.x,
        y: data.y
    })
};

function playerById(id) {
    var i;
    for (i = 0; i < players.length; i++) {
        if (players[i].id == id)
            return players[i];
    };

    return false;
};

function Player(xpos, ypos) {
    var result = {
        x: xpos,
        y: ypos,
        id: 0
    }
    return result;
}

path --

1 个答案:

答案 0 :(得分:2)

在您分享的屏幕截图中,文件夹名称为Public而不是public,在osX中(我假设您正在使用屏幕截图),Public和{{ 1}}是不同的。

如果你写这个,

public

事情应该开始发挥作用。

另外,如果你想设置一个默认页面,即当用户访问app.use(express.static(__dirname + '/Public'));并希望你的/被提供时,你可以这样做

/index.html

我希望这可以解决你的问题!!