I have a site in express and am trying to convert it to use a routes.js file to clean things up. I am getting a TypeError, explained below. I have read this and this but I still can't figure it out. Currently the site works with the following lines:
const server = express()
.set('view engine', 'ejs') // set up ejs for templating
.use(flash()) // use connect-flash for flash messages stored in session
.use((req, res) => res.render('../views/pages/indextimewithall.ejs', {stockSearched :"X",
activeStocks: [],
addingError:false}) )
.listen(port);
const wss = new SocketServer({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
});
setInterval(() => {
wss.clients.forEach((client) => {
client.send(new Date().toTimeString());
});
}, 1000);
But when I change it to this:
const server = express()
.set('view engine', 'ejs') // set up ejs for templating
.use(flash()) // use connect-flash for flash messages stored in session
.listen(port);
require('./app/routes.js')(server);
const wss = new SocketServer({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
});
setInterval(() => {
wss.clients.forEach((client) => {
client.send(new Date().toTimeString());
});
}, 1000);
..I get the TypeError: app.get is not a function
Here are the contents of routes.js file:
// app/routes.js
module.exports = function(app) {
app.get('/', function(req, res) {
var myStocks = require('./models/myStock');
var showStocks = function(err, data){
res.render('pages/indextimewithall.ejs', {
stockSearched :"X",
activeStocks: [],
addingError:false
});
}
myStocks.find({isActive:true}).exec(showStocks);
});
};
Thank you for any suggestions.