Skip to content

Query hangs when used as express middleware #941

@ext4cats

Description

@ext4cats

I've been trying to use this library as an express middleware, for dependency injection convenience.

But I ran into a roadblock; queries get stuck seemingly forever when I do this. I am not sure why this happens? I've tested the code over and over and the issue persists. I moved off my production project to try reproducing it and was able to do it.

Here's some sample code, running on Node LTS with Postgres.js 3.4.4 and Express 4.20.0.

import express from 'express';
import postgres from 'postgres';

function createExpressApp() {
  const app = express();
  app.use(databaseMiddleware());
  app.get('/', testHandler);
  return app;
}

const databaseMiddleware = () => {
  const sql = postgres(
    'postgresql://postgres:postgres@localhost:8080/postgres',
  );
  return (req, res, next) => {
    req.sql = sql;
    next();
  };
};

const testHandler = async (req, res, next) => {
  try {
    const response = await req.sql`SELECT version()`;
    res.status(200).json(response);
  } catch (err) {
    next(err);
  }
};

createExpressApp().listen(8080, () => console.log("listenin'"));

I hope this gets fixed if it really is a bug; feels like a shame to not be able to use such a nice library because of such an obnoxious issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions