Open SatioO opened 7 years ago
I am having the same issue. Node Version - 6.11.2 NPM Version - 3.10.10 Express - 4.14.0 Windows 10
import * as express from "express";
import * as helmet from "helmet";
import * as morgan from "morgan";
import * as compression from "compression";
import * as bodyParser from "body-parser";
import * as fs from "fs";
import * as spdy from "spdy";
export class Server {
app = express();
options = {
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
};
constructor(private port: number) {
this.configureMiddleware();
this.configureRoutes();
}
private configureMiddleware() {
console.info("Configuring middleware...");
this.app.use(helmet());
this.app.use(compression());
this.app.use(bodyParser.urlencoded({ extended: true }));
this.app.use(bodyParser.json());
this.app.use(express.static('public'));
this.app.use(morgan('dev'));
}
private configureRoutes() {
console.info("Configuring routes...");
}
public start() {
spdy
.createServer(this.options, this.app)
.listen(this.port, (error:any) => {
if (error) {
console.error(error);
return process.exit(1);
} else {
console.log('Listening on port: ' + this.port + '.');
}
});
}
}
May be an issue not because of this module itself, but the typed definitions written for it ... https://github.com/DefinitelyTyped/DefinitelyTyped/blob/354cec620daccfa0ad167ba046651fb5fef69e8a/types/spdy/index.d.ts
Are there any recommendations as to how we can resolve this issue?
Thanks
@SatioO Were you able to figure out how to fix this issue?
Having the same issue setting up spdy with express.
import { readFileSync } from 'fs';
import api from './api';
const spdy = require('spdy');
const mongoose = require('mongoose');
require('dotenv').config();
/* Instantiate our app instance */
const app: api = new api();
/* required for ssl encryption and http2 */
const options = {
key: readFileSync(__dirname + '/../server.key'),
cert: readFileSync(__dirname + '/../server.crt')
};
/* Get current environment */
const env = app.currentEnv();
let DATABASE_URL;
let PORT;
/* set environment variables */
if (env === 'production') {
DATABASE_URL = process.env.MONGODB_URI;
PORT = process.env.PORT;
} else {
DATABASE_URL = process.env.TEST_DATABASE_URL;
PORT = 3000;
}
/* Set mongoose promise to native ES6 promise */
mongoose.Promise = global.Promise;
const connectOptions = {
useMongoClient: true,
keepAlive: true,
reconnectTries: Number.MAX_VALUE
};
/* Both runServer and closeServer need access to the server var,
* so it's declared outside of both function.
*/
let server;
export const runServer = async (
databaseUrl: string = DATABASE_URL,
port: number | string = PORT
) => {
try {
await mongoose.connect(databaseUrl, connectOptions);
await new Promise((resolve, reject) => {
server = spdy
.createServer(options, app)
.listen(port, () => {
console.info(
`Your server is listening on port ${port} with the db ${databaseUrl} in a ${env} environment🤔`
);
resolve();
})
.on('error', err => {
mongoose.disconnect();
reject(err);
});
});
} catch (err) {
console.error(err);
}
};
export const closeServer = async () => {
try {
await mongoose.disconnect();
await new Promise((resolve, reject) => {
console.info(`Closing server. Goodbye old friend.`);
server.close(err => {
if (err) return reject(err);
return resolve();
});
});
} catch (err) {
console.error('error');
}
};
if (require.main === module) {
runServer().catch(err => console.error(err));
}
Throws error TypeError: "listener" argument must be a function
at the line where I call .createServer()
@chrstntdd the app parameter in .createServer(options, app) needs to be an instance of express() https://webapplog.com/http2-node/
@chrstntdd the app parameter in .createServer(options, app) needs to be an instance of express() https://webapplog.com/http2-node/
It is year 2021, ans I still encounter this problem. I tried setting app as instance of Express but the returned error is the same as usual:
TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object at checkListener (events.js:112:11) at _addListener (events.js:348:3) at Server.addListener (events.js:406:10)
Tried to plug in spdy in my exsiting angular project. It is running well with the existing node http and https module. but when I run same code with spdy, It fails to start.
Below is my code and error response:
and here is my typescript defination file:
{ "compilerOptions": { "moduleResolution": "node", "module": "es2015", "target": "es5", "noImplicitAny": false, "sourceMap": false, "experimentalDecorators": true, "emitDecoratorMetadata": true, "lib": [ "es2016", "dom" ] }, "include": [ "src" ] }