gpt4 book ai didi

node.js - supertest 无法解析状态码 400

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:02 24 4
gpt4 key购买 nike

我在 supertest、mocha 和 Node 方面遇到了一些问题,解析状态代码为 400。

这是我的 index.js 代码:

var express = require('express');
var app = express();

app.get('/api/tweets', function(req, res) {
var userId = req.query.userId;

if (!userId) {
return res.sendStatus(400);
}

});

module.exports = app;

当我执行 mocha test.js 时,这是我得到的打印输出:

Test:
1) respond with status code 400 for GET /api/tweets with missing userId


0 passing (75ms)
1 failing

1) Test: respond with status code 400 for GET /api/tweets with missing userId:
Uncaught TypeError: Cannot read property 'status' of undefined
at net.js:1419:10

这是 test.js 的代码:

var request = require('supertest')

describe('Test:', function() {
var server = require('./index')
it('respond with status code 400 for GET /api/tweets with missing userId', function(done) {
request(server)
.get('/api/tweets?userId')
.expect(400, done)
})
})

有没有可以指点一下的?我不确定为什么 Node/ super 测试会给我这样一个奇怪的错误。

我的设置如下:

NVM 已安装,但使用的是系统 Node 版本 (0.12.3)。

预先感谢您的帮助!

-斯图。

最佳答案

Supertest ... Cannot read property 'status' of undefined

通过安全连接进行测试时,我遇到了上述错误。我通过在测试运行中添加 NODE_TLS_REJECT_UNAUTHORIZED=0 作为环境变量来修复此问题。

对于那些想知道为什么会发生这种情况的人来说,发生这种情况的原因可能是“127.0.0.1”未在您的证书中列为有效 IP。根本错误是:

Hostname/IP doesn't match certificate's altnames: "IP: 127.0.0.1 is not in the cert's list:

但是,您不会看到此错误消息,只会看到虚假且令人讨厌的“无法读取未定义的属性“状态””。

关于node.js - supertest 无法解析状态码 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com