O
O
OKNOZA2015-03-11 19:57:57
Node.js
OKNOZA, 2015-03-11 19:57:57

What kind of errors does NodeJS give?

When running the script, node gives errors, how to solve them?
A normal HTTP test server outputs and launches fine.
Operating system Ubuntu.

/var/node/hello.js:1
(function (exports, require, module, __filename, __dirname) { <!DOCTYPE html>
                                                              ^
SyntaxError: Unexpected token <
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OKNOZA, 2015-03-11
@OKNOZA

/var/node/client.js:9
window.onload = function() {
^
ReferenceError: window is not defined
    at Object.<anonymous> (/var/node/client.js:9:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

strings = {
    'connected': '[sys][time]%time%[/time]: Вы успешно соединились к сервером как [user]%name%[/user].[/sys]',
    'userJoined': '[sys][time]%time%[/time]: Пользователь [user]%name%[/user] присоединился к чату.[/sys]',
    'messageSent': '[out][time]%time%[/time]: [user]%name%[/user]: %text%[/out]',
    'messageReceived': '[in][time]%time%[/time]: [user]%name%[/user]: %text%[/in]',
    'userSplit': '[sys][time]%time%[/time]: Пользователь [user]%name%[/user] покинул чат.[/sys]'
};
window.onload = function() {
    if (navigator.userAgent.toLowerCase().indexOf('chrome') != -1) {
        socket = io.connect('http://127.0.0.1:8080', {'transports': ['xhr-polling']});
    } else {
        socket = io.connect('http://127.0.0.1:8080');
    }
    socket.on('connect', function () {
        socket.on('message', function (msg) {
            // Добавляем в лог сообщение, заменив время, имя и текст на полученные
            document.querySelector('#log').innerHTML += strings[msg.event].replace(/\[([a-z]+)\]/g, '<span class="$1">').replace(/\[\/[a-z]+\]/g, '</span>').replace(/\%time\%/, msg.time).replace(/\%name\%/, msg.name).replace(/\%text\%/, unescape(msg.text).replace('<', '&lt;').replace('>', '&gt;')) + '<br>';
            // Прокручиваем лог в конец
            document.querySelector('#log').scrollTop = document.querySelector('#log').scrollHeight;
        });
        document.querySelector('#input').onkeypress = function(e) {
            if (e.which == '13') {
                socket.send(escape(document.querySelector('#input').value));
                // Очищаем input
                document.querySelector('#input').value = '';
            }
        };
        document.querySelector('#send').onclick = function() {
            socket.send(escape(document.querySelector('#input').value));
            document.querySelector('#input').value = '';
        };		
    });
};

A
Alexey Yaroshevich, 2015-03-12
@qfox

Obviously, /var/node/hello.js:1 is HTML here, but should be javascript.
/var/node/client.js - this file needs to be forwarded to the client in some way (either nginx, or using express and the static middleware).
You are clearly not doing what nodejs expects from you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question