app.use is not a function
See original GitHub issueHello there, i’m trying to use express-ws together with already existed express application, but running into some issues.
Basic info: Node.js - v6.9.1 Express - 4.15.0 Express-ws - 3.0.0
Using express with Gulp throw nodemon:
gulp.task('express:dev', function(cb) {
var started = false;
return nodemon({
script: 'server/app.js',
watch: ['server/app.js']
}).on('start', function() {
// to avoid nodemon being started multiple times
// thanks @matthisk
if (!started) {
cb();
started = true;
}
}).on('restart', function(ev) {
gulpUtil.log('[nodemon] restarted');
gulpUtil.log('files changed:\n' + ev);
setTimeout(function() {
browserSync.notify('reloading now ...');
browserSync.reload({stream: false});
}, 1000);
})
.on('crash', function() {
gulpUtil.log('[nodemon] crashed: script crashed for some reason');
})
.on('exit', function() {
gulpUtil.log('[nodemon] exited cleanly');
});
});
app.js:
var NODE_PORT = 3000;
var express = require('express');
var expressWs = require('express-ws');
var logger = require('./config/logger');
// Setup server
var app = express();
app.server = require('http').createServer(app);
var expressWsObj = expressWs(express, app.server);
require('./config/express')(app);
require('./routes')(app);
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
});
console.log('socket', req.testing);
});
// Start express server
app.server.listen(NODE_PORT, 'localhost', function() {
'use strict';
logger.verbose('Express server serves the fake backend and listens on port', NODE_PORT);
});
// Expose app
module.exports = app;
Ends up with next error: error: uncaughtException: app.use is not a function date=Thu Mar 02 2017 09:33:57 GMT+0100 (Romance Standard Time), pid=10052, uid=null, gid=null, cwd=src\main\webapp, execPath=C:\Program Files\nodejs\node.exe, version=v6.9.1, argv=[C:\Program Files\nodejs\node.exe, src\main\webapp\server\app.js], rss=33292288, heapTotal=21008384, heapUsed=13176632, loadavg=[0, 0, 0], uptime=1793.9372317, trace=[column=9, file=src\main\webapp\server\config\express.js, function=module.exports, line=12, method=exports, native=false, column=28, file=src\main\webapp\server\app.js, function=, line=20, method=null, native=false, column=32, file=module.js, function=Module._compile, line=570, method=_compile, native=false, column=10, file=module.js, function=Object.Module._extensions…js, line=579, method=Module._extensions…js, native=false, column=32, file=module.js, function=Module.load, line=487, method=load, native=false, column=12, file=module.js, function=tryModuleLoad, line=446, method=null, native=false, column=3, file=module.js, function=Function.Module._load, line=438, method=Module._load, native=false, column=10, file=module.js, function=Module.runMain, line=604, method=runMain, native=false, column=7, file=bootstrap_node.js, function=run, line=394, method=null, native=false, column=9, file=bootstrap_node.js, function=startup, line=149, method=null, native=false], stack=[TypeError: app.use is not a function, at module.exports (src\main\webapp\server\config\express.js:12:9), at Object.<anonymous> (src\main\webapp\server\app.js:20:28), at Module._compile (module.js:570:32), at Object.Module._extensions…js (module.js:579:10), at Module.load (module.js:487:32), at tryModuleLoad (module.js:446:12), at Function.Module._load (module.js:438:3), at Module.runMain (module.js:604:10), at run (bootstrap_node.js:394:7), at startup (bootstrap_node.js:149:9)]
Any idea why express use function are not visible anymore ?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
The error seems to be occurring on line 12 in
./config/express
. Could you include that file as well? Nothing seems immediately wrong in the code you’ve pasted, so I’d expect the issue to be there (sinceexpress-ws
doesn’t do anything withapp.use
).var expressWsObj = expressWs(app, app.server);