简体   繁体   English

错误:连接ECONNREFUSED 127.0.0.1:3100

[英]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. 我是node的新手,运行npm test时收到错误:Error:connect ECONNREFUSED 127.0.0.1:3100

This is my test file. 这是我的测试文件。

Test.ts 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();
    }
});
});
});

This is my app file 这是我的应用程序文件

app.ts 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;

Your server is not running while trying to test GET /hello route which is why it fails to connect. 您的服务器在尝试测试GET / hello路由时未运行,因此无法连接。

See here an example on how you should test your API's server routes. 请参阅此处的示例 ,了解如何测试API的服务器路由。

Within your test.ts file, you should import your server and make sure it listen before tests and close after tests. 在您的test.ts文件中,您应该导入服务器,并确保它在测试之前监听并在测试之后关闭

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');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM