Json-server: TypeError: server.close is not a function

Created on 25 Sep 2017  路  1Comment  路  Source: typicode/json-server

I am trying to use with with mocha tests and I am finding that there's no way to close the express server after my tests complete. This causes issues with other tests that try to bind to the same port and fail because jsonServer is still running.

Most helpful comment

I solved my problem by returning app.listen to server so I can close

var expect = require('expect.js'),
  jsonServer = require('json-server'),
  path = require('path'),
  server = {};

describe('sports service', () => {
  before(() => {
    // Starting the server
    var app = jsonServer.create();
    const router = jsonServer.router(path.join(__dirname, '../../../data/sports.db.json'));
    const middlewares = jsonServer.defaults();
    const version = 'v3'
    const port = 8080;
    var sportId = 'australian-football';
    var leagueId = 'afl';

    app.use(middlewares);
    app.use(jsonServer.bodyParser);
    app.use(`/${version}/sports/${sportId}/${leagueId}`, router);
    server = app.listen(port, () => {
      // console.log('JSON Server is running');
    });
  });

  after(() => {
    server.close();
  })

>All comments

I solved my problem by returning app.listen to server so I can close

var expect = require('expect.js'),
  jsonServer = require('json-server'),
  path = require('path'),
  server = {};

describe('sports service', () => {
  before(() => {
    // Starting the server
    var app = jsonServer.create();
    const router = jsonServer.router(path.join(__dirname, '../../../data/sports.db.json'));
    const middlewares = jsonServer.defaults();
    const version = 'v3'
    const port = 8080;
    var sportId = 'australian-football';
    var leagueId = 'afl';

    app.use(middlewares);
    app.use(jsonServer.bodyParser);
    app.use(`/${version}/sports/${sportId}/${leagueId}`, router);
    server = app.listen(port, () => {
      // console.log('JSON Server is running');
    });
  });

  after(() => {
    server.close();
  })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sboudouk picture sboudouk  路  3Comments

AdamCook44 picture AdamCook44  路  3Comments

pantchox picture pantchox  路  3Comments

sharpmachine picture sharpmachine  路  4Comments

bahmutov picture bahmutov  路  3Comments