• Partager sur Facebook
  • Partager sur Twitter

Node.js and

"Cannot POST /" issue in node.js + app

    7 janvier 2016 à 12:37:57


    I try to code the last exercise in the node tutorial. But, I meet some issue when I run the app. My browser (chrome), throw "Cannot POST /" on the window, and in the console I get "Failed to load resource: the server responded with a status of 404 (Not Found)". Here what my code looks like:


    var app = require('express')(),
        server = require('http').createServer(app),
        io = require('').listen(server),
        ent = require('ent'), // For security purpose
    // Loading the web page
    app.get('/', function(req, res) {
        res.sendFile('views/index.html', {root : __dirname});
        io.sockets.on('connection', function(socket, username) {
        // When the username is received, it's stored as a session variable 
        //and we inform the other people
           socket.on('new-client', function(username) {
            username = ent.encode(username);
            socket.username = username;
            socket.broadcast.emit('new-client', username);
        // When a message is received, the username is retrieved and sent to other people
            socket.on('message', function(message) {
            message = ent.encode(message);
            socket.broadcast.emit('message', {username: socket.username, message: message});

    - client

    <!DOCTYPE html>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <title>Super Chat</title>
            <meta name="description" content="My first super chat with">
              * { margin: 0; padding: 0; box-sizing: border-box; }
              body { font: 13px Helvetica, Arial; }
              form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
              form #message { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
              form #send-message { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
              #chat-zone p { margin: 0; padding: 0; }
              #chat-zone p { padding: 5px 10px; }
              #chat-zone p:nth-child(odd) { background: #eee; }
            <h1>REAL-TIME SUPER CHAT!</h1>
            <section id="chat-zone"></section>
            <form action="/" method="post" id="chat-form">
                <input type="text" name="message" id="message" autofocus>
                <input type="submit" id="send-message" value="send">
            <script type="text/javascript" src="/"></script>
            <script type="text/javascript">
                // Connection to
                var socket = io.connect('http://localhost:8080');
                var chatZone = document.getElementById('chat-zone');
                var chatForm = document.getElementById('chat-form');
                var chatMessage = document.getElementById('message');
                var username = prompt('Your Pseuso');
                socket.emit('new-client', username);
                document.title = username + '-' + document.title;
                // When a message is received, we display it in the chat-zone
                socket.on('message', function(data) {
                    insertMessage(data.username, data.message)
                // When a client is connected, we display his name in the chat-zone
                socket.on('new-client', function(username) {
                    chatZone.prependChild('<p><em>' + username + ' has joined the chat!</em></p>')
                // When we send our form, the message is received, displayed on our page and sent to other people
                chatForm.submit(function() {
                    var message = chatMessage.getElementValue();
                    socket.emit('message', message);
                    insertMessage(username, message);
                // Get an element value
                HTMLElement.prototype.getElementValue = function() {
                    var text = this.innerText || this.textContent;
                    return text;
                // Prepend element in the DOM
                HTMLElement.prototype.prependChild = function(childNode) {
                    this.firstChild ? this.insertBefore(childNode, this.firstChild) : this.appendChild(childNode);
                // Add message to the chat-zone
                function insertMessage(username, message) {
                    chatZone.prependChild('<p><strong>' + username + '</strong>' + message + '</p>');

    I work on it three days long. Can someone help fix it? Thanks

    • Partager sur Facebook
    • Partager sur Twitter
      13 janvier 2016 à 22:31:17


      Normally if you copy/paste correctly the code of the tutorial, it work good, after i'm not good in node.js but if i look the code in the tutorial you have forgot to declare the fs variable in server code ?

      fs = require('fs');

      Edité par Milan_0512 13 janvier 2016 à 22:31:47

      • Partager sur Facebook
      • Partager sur Twitter

      Node.js and

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown