错误:连接 ECONNREFUSED 127.0.0.1:3100 [英] Error: connect ECONNREFUSED 127.0.0.1:3100

查看:168
本文介绍了错误:连接 ECONNREFUSED 127.0.0.1:3100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 node 的新手.当我运行 npm test 时,我收到了 Error: connect ECONNREFUSED 127.0.0.1:3100 的错误.

I am new to node.I get the error that is Error: connect ECONNREFUSED 127.0.0.1:3100, when I run npm test.

这是我的测试文件.

Test.ts

import * as chai from 'chai';
let chaiHttp = require('chai-http');
import * as assert from 'assertthat';
import * as request from "superagent";
chai.use(chaiHttp);
const expect = chai.expect;
describe('Checking whether the response return status 200', function() {
it('Status OK', function(done) {
return chai.request('https://localhost:3100') 
.get('/hello')
.end(function(err, res){
    if(err){
        done(err);
    }
    else{
        expect(res.body.message).to.equal('hello world');
        done();
    }
});
});
});

这是我的应用文件

app.ts

import * as express from 'express';
import {Request,Response} from 'express';
const app: express.Express = express();

app.get('/hello',(req:Request,res:Response)=>{
    res.json({
        message:"hello world"
    });
});
app.listen(3100,() =>{
    console.log("server listening");
});
export default app;

推荐答案

您的服务器在尝试测试 GET/hello 路由时没有运行,这就是它无法连接的原因.

Your server is not running while trying to test GET /hello route which is why it fails to connect.

在这里查看 示例 关于如何测试 API 的服务器路由.

See here an example on how you should test your API's server routes.

在您的 test.ts 文件中,您应该导入您的服务器并确保它在测试前监听并在测试后关闭.>

Within your test.ts file, you should import your server and make sure it listen before tests and close after tests.

import server from 'app.ts';
import * as chai from 'chai';
import * as assert from 'assertthat';
import * as request from 'superagent';

const chaiHttp = require('chai-http');
const expect = chai.expect;

chai.use(chaiHttp);

describe('Checking whether the response return status 200', function() {
    it('Status OK', async function(done) {
        const { res, err } = await chai.request(server).get('/hello');
        expect(res.body.message).to.equal('hello world');
    });
});

这篇关于错误:连接 ECONNREFUSED 127.0.0.1:3100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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