Node.js NodeJS无法向客户端发送消息

Node.js NodeJS无法向客户端发送消息,node.js,Node.js,我试图学习Node.JS,但使用本教程制作REST API: 我有一个很愚蠢的小问题,我似乎无法解决。在我的user_routes.js文件中,我试图向express()应用程序写入几条消息,但是在第一次调用res.send()后,它就不起作用了。为什么会这样?我在代码中找不到要关闭连接的地方,所以为什么我不能多次写入请求 我的用户_routes.js module.exports = function(app, db) { app.post('/user', function

我试图学习Node.JS,但使用本教程制作REST API:

我有一个很愚蠢的小问题,我似乎无法解决。在我的user_routes.js文件中,我试图向express()应用程序写入几条消息,但是在第一次调用res.send()后,它就不起作用了。为什么会这样?我在代码中找不到要关闭连接的地方,所以为什么我不能多次写入请求

我的用户_routes.js

    module.exports = function(app, db) {
    app.post('/user', function (req,res) {
        res.send("User Request Recieved via POST");

        // Add the user to the database, if they don't already exist
        const firstName = req.body.firstName;
        const lastName = req.body.lastName;
        const email = req.body.email;
        const password = req.body.password;

        const user = {
            firstName: firstName,
            lastName : lastName,
            email : email,
            password : password
        };

        if (db.collection('users').find({'email':email}).count() == 0) {
            res.send('Unique Email');
            db.collection('users').insert(user, (err, result) => {
                if (err) {
                    console.log("error");
                } else {
                    console.log(result.ops[0])
                }
            });
        } else {
            res.send("Email already in use")
        }

    })
};
任何my server.js:

const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const app = express();
const port = 6969;
const db = require('./config/db')

// We need to decode data from the url using the body-parser lib
app.use(bodyParser.urlencoded({ extended: true }));

MongoClient.connect(db.url).then( function(db) {
    require('./app/routes')(app, db);
    app.listen(port, () => {
      console.log('We are live on ' + port);
    });               
}).catch (function (err) {
    console.log(err);
});     


module.exports = app;
我似乎没有关闭任何地方的连接,为什么我只能向客户端写入一条消息?

res。send()是函数最不应该做的事情。将其视为函数的返回,不能多次返回。

res.send()是函数最不应该做的事情。将其视为函数的返回,不能多次返回。

res.send()==return() res.send()相当于帖子的“return”——每次调用只能执行一次

每个res.send()有多条消息 如果要通过一个调用发送多条消息,则需要编译要发送的消息的对象/数组,并通过res.send()发送该对象/数组。例如:

ret_msg = [];
ret_msg.push("Here's your first message.");
ret_msg.push("Here's your second message.");
ret_msg.push("Here's your third message.");

res.send(ret_msg);
res.send()==return() res.send()相当于帖子的“return”——每次调用只能执行一次

每个res.send()有多条消息 如果要通过一个调用发送多条消息,则需要编译要发送的消息的对象/数组,并通过res.send()发送该对象/数组。例如:

ret_msg = [];
ret_msg.push("Here's your first message.");
ret_msg.push("Here's your second message.");
ret_msg.push("Here's your third message.");

res.send(ret_msg);

@DtrollMC这在node.js中不是问题。客户端向服务器询问一个“问题”,服务器需要为其提供一个响应。否则,客户端将不知道服务器何时完成响应。如果要发送更复杂的消息,则应将所有信息附加到对象。大多数语言中的大多数函数都是这样工作的。@DtrollMC node.js中没有这个问题。客户端向服务器询问一个“问题”,服务器需要为其提供一个响应。否则,客户端将不知道服务器何时完成响应。如果要发送更复杂的消息,则应将所有信息附加到对象。这就是大多数语言中大多数函数的工作方式。