nginx 从应用程序nodejs收到的响应不完整

93ze6v8z  于 2023-05-16  发布在  Nginx
关注(0)|答案(2)|浏览(199)

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

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);
busg9geu

busg9geu1#

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

syqv5f0l

syqv5f0l2#

如果我们想知道console.log中的错误

.htaccess

...

Options +FollowSymLinks -Indexes
IndexIgnore *
DirectoryIndex
<IfModule mod_rewrite.c>
RewriteEngine on
# Simple URL redirect:
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P]
</IfModule>

相关问题