从应用程序 nodejs 收到的不完整响应 [英] incomplete response received from application nodejs

查看:69
本文介绍了从应用程序 nodejs 收到的不完整响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 nginx 服务器上使用 nodejs.有时节点应用程序崩溃并返回从应用程序收到的不完整响应".是什么导致了这个问题?

I'm using nodejs on nginx server. Sometimes the node app is crashing and returning 'incomplete response received from application'. What is causing this problem?

const Express = require('express');
const BodyParser = require('body-parser');
const Request = require('request');
const Conf = require('./conf');
const {db} = require('./lib/database');
const app = Express();

app.use(BodyParser.json());
app.use(BodyParser.urlencoded({extended: false}));

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.header('Access-Control-Allow-Methods', 'POST');
    if ('OPTIONS' == req.method) {
        res.header('Access-Control-Allow-Methods', 'POST');
        return res.sendStatus(200);
    }
    next();
});

app.post('/getProperty', (req, res) => {
    const sql = "SELECT ('['||(st_asgeojson(geom)::json)||']')::json FROM spt.spt  where id=" + req.body.id;
    db.query(sql, (err, result) => {
        if (err) res.status(err.code).json({code: err.code, message: err.message}).end();
        (result.rows.length == 0) ? res.send([]) : res.send(result.rows[0].json);
    })
});

app.post('/getAnalysis', (req, res) => {
    const sql = "select value from test.test where id=" + req.body.id + " order by value asc";
    db.query(sql, (err, result) => {
        if (err) result.status(err.code).json({code: err.code, message: err.message}).end();
        res.send(result.rows);
    })
});

app.listen(3000);

推荐答案

如果发生错误,您的代码会尝试发送响应两次.您需要在向客户端发送错误响应后退出该函数,因此要么在您的 if 中使用 return 或在下面添加一个 else成功响应.发送多个响应将在 express 中断开套接字.

If an error occurs, your code tries sending a response twice. You need to exit the function after sending the error response to the client, so either use return in your if or add an else below for the success response. Sending multiple responses will dicsonnect the socket in express.

这篇关于从应用程序 nodejs 收到的不完整响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆