Node.js “无法发布/”nodeJS

Node.js “无法发布/”nodeJS,node.js,express,socket.io,Node.js,Express,Socket.io,我想用nodeJS&socket.io实时开发一个待办事项列表 我可以读取任务,但在插入任务时,出现POST错误。无法POST/但服务器正确接收了任务,因为我可以在控制台日志中读取任务并返回到本地主机,任务被添加。。。我认为我必须把我的帖子做错,但我不知道如何正确地做。 这是我的密码 index.html: <body>         <h1>Todo list temps réel !</h1>           <form action="/"

我想用nodeJS&socket.io实时开发一个待办事项列表

我可以读取任务,但在插入任务时,出现POST错误。无法POST/但服务器正确接收了任务,因为我可以在控制台日志中读取任务并返回到本地主机,任务被添加。。。我认为我必须把我的帖子做错,但我不知道如何正确地做。 这是我的密码

index.html:

<body>
        <h1>Todo list temps réel !</h1>
 
        <form action="/" method="post" id="formulaire_tache">
            <input type="text" name="message" id="tache" placeholder="Votre tache..." size="50" autofocus />
            <input type="submit" id="envoi_tache" value="Envoyer" />
        </form>
 
        <section id="zone_todo">
          <ul>
 
          </ul>
        </section>
 
 
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="/socket.io/socket.io.js"></script>
        <script>
 
            // Connexion à socket.io
            var socket = io.connect('http://localhost:8080');
 
            // On declenche l'evenement nouveau_client
            socket.emit('nouveau_client');
 
            socket.on('initTodo', function(todolist){
              //On recupere les taches
              console.log('Client');
              todolist.forEach(function(tache, index){
                $('#zone_todo').prepend('<li><a href="/todo/supprimer/' + index + '">✘</a>' + tache + '</li>');
              })
            })
 
            // Lorsqu'on envoie le formulaire, on transmet le message et on l'affiche sur la page
            $('#formulaire_tache').submit(function () {
                var message = $('#tache').val();
                socket.emit('ajout', message); // Transmet la tache au serveur
                insereMessage(message); // Affiche la tache aussi sur notre page
                $('#tache').val('').focus(); // Vide la zone et remet le focus dessus
                return false; // Permet de bloquer l'envoi "classique" du formulaire
            });
 
            // Ajoute un message dans la page
            function insereMessage(tache) {
                $('#zone_todo').prepend('<li><a href="/todo/supprimer/' + index + '">✘</a>' + tache + '</li>');
            }
        </script>
    </body>
和我的控制台日志:

node app.js 

[ 'test', 'test2' ]

tache ajouter azerty

[ 'test', 'test2', 'azerty' ]
感谢您的帮助

您不能使用return false来阻止提交的正常行为。 因此,调用到达的节点没有/的post方法

您必须使用:

$('#formulaire_tache').submit(function (event) {

      event.preventDefault();   // you must use this!

      var message = $('#tache').val();
      socket.emit('ajout', message); // Transmet la tache au serveur
      insereMessage(message); // Affiche la tache aussi sur notre page
      $('#tache').val('').focus(); // Vide la zone et remet le focus dessus
});
POST的Nodejs代码段

NodeJS本地

NodeJS请求

单叶树


我看不出您在服务器代码中的什么地方监听帖子。根据代码,您似乎也没有发送帖子。应防止表单提交。我猜可能是发生了JS错误,从而阻止了返回false。你认为,我需要用socket.IO听一篇帖子吗?我在想不仅仅是为了快递?不,我不认为你们需要听这篇帖子。thta可能是另一个问题的症状,sch没有阻止表单提交。谢谢,但它不起作用,这一次,当我返回localhostnevermind时,任务没有被添加。如果,我删除表单上的method=post,它可以处理您的代码,也可以处理我的代码。谢谢:
$('#formulaire_tache').submit(function (event) {

      event.preventDefault();   // you must use this!

      var message = $('#tache').val();
      socket.emit('ajout', message); // Transmet la tache au serveur
      insereMessage(message); // Affiche la tache aussi sur notre page
      $('#tache').val('').focus(); // Vide la zone et remet le focus dessus
});
var http = require("http");

var options = {
  "method": "POST",
  "hostname": "localhost",
  "port": "8080",
  "path": "/api/subject",
  "headers": {
    "content-type": "application/json",
    "cache-control": "no-cache"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ 
  author: "balu",
  title: 'Test NodeJS  POST' }));

req.end();
    var request = require("request");

    var options = { method: 'POST',
      url: 'http://localhost:8080/api/subject',
      headers: 
       { 'cache-control': 'no-cache',
         'content-type': 'application/json' },
      body: 
       { author: 'Balu',
         title: 'NodeJS Post' },
      json: true };

    request(options, function (error, response, body) {
      if (error) throw new Error(error);

      console.log(body);
    });
var unirest = require("unirest");

var req = unirest("POST", "http://localhost:8080/api/subject");

req.headers({
  "cache-control": "no-cache",
  "content-type": "application/json"
});

req.type("json");
req.send({
  "author": "Balu",
  "title": "NodeJs Post"
});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});